Assistance for some codes in c sharp - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Assistance for some codes in c sharp

IzooIzoo Posts: 1Member
I request for codes that will enable me to add new,delete and save in the database that has already been connected to my application form in c sharp,and am using oracle database.

Comments

  • nivp4mnivp4m Posts: 14Member
    M providing u code for SQL server. You just have to replace sql string connection with Oracle connection string.
    Take appropriate text boxes(6 text box),picture box,buttons(6 buttons)
    Reply me if u have ny prob.
    SEE D ATTACHMENT


    [code]using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;

    namespace WindowsFormsApplication4
    {
    public partial class Form2 : Form
    {
    public Form2()
    {
    InitializeComponent();
    }

    SqlConnection con = new SqlConnection("data source=LAB-387;initial catalog=2to3;integrated security=true");
    SqlDataReader dr;
    SqlCommand cmd = new SqlCommand();
    int r,r1;
    string qry,fname;


    private void Form2_Load(object sender, EventArgs e)
    {
    con.Open();
    cmd.Connection = con;
    cmd.CommandText = "select * from student";
    dr = cmd.ExecuteReader();

    while (dr.Read())
    {

    comboBox1.Items.Add(dr.GetValue(0).ToString());
    //treeView1.Nodes.Add(dr.GetValue(0).ToString());
    treeView1.Nodes.Add(dr["rno"].ToString());
    }

    con.Close();
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    try
    {
    r = int.Parse(comboBox1.Text);
    r1 = r;
    qry = "select * from student where rno=" + r;
    //MessageBox.Show(qry);
    con.Open();
    cmd.Connection = con;
    cmd.CommandText = qry;
    dr = cmd.ExecuteReader();
    if (dr.Read())
    {
    textBox1.Text = dr.GetValue(0).ToString();
    textBox2.Text = dr.GetValue(1).ToString();
    textBox3.Text = dr.GetValue(2).ToString();
    textBox4.Text = dr.GetValue(3).ToString();
    textBox5.Text = dr.GetValue(4).ToString();
    fname = dr.GetValue(5).ToString();
    if (File.Exists(fname))
    pictureBox1.Image = Image.FromFile(fname);
    }

    }
    catch (Exception e1)
    {
    }
    con.Close();

    }

    private void button1_Click(object sender, EventArgs e)
    {
    textBox2.Text = "";
    textBox3.Text = "";
    textBox4.Text = "";
    textBox5.Text = "";
    r = int.Parse(textBox1.Text);
    r1 = r;
    qry = "select * from student where rno=" + r;

    con.Open();
    cmd.Connection = con;
    cmd.CommandText = qry;
    dr = cmd.ExecuteReader();

    if (dr.Read())
    {
    textBox2.Text = dr.GetValue(1).ToString();
    textBox3.Text = dr.GetValue(2).ToString();
    textBox4.Text = dr.GetValue(3).ToString();
    textBox5.Text = dr.GetValue(4).ToString();
    fname = dr.GetValue(5).ToString();
    if (File.Exists(fname))
    pictureBox1.Image = Image.FromFile(fname);
    // textBox5.Text=dr[4].ToString();
    // textbBox2.Text=dr["sname"].ToString();
    // int b = dr.GetInt32(2);
    // int c = Convert.ToInt32(dr.GetValue(1).ToString());
    }
    else
    MessageBox.Show(r + " not found");
    con.Close();
    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
    r = int.Parse(e.Node.Text);
    r1 = r;
    qry = "select * from student where rno=" + r;

    con.Open();
    cmd.Connection = con;
    cmd.CommandText = qry;
    dr = cmd.ExecuteReader();
    if (dr.Read())
    {
    textBox1.Text = dr.GetValue(0).ToString();
    textBox2.Text = dr.GetValue(1).ToString();
    textBox3.Text = dr.GetValue(2).ToString();
    textBox4.Text = dr.GetValue(3).ToString();
    textBox5.Text = dr.GetValue(4).ToString();
    fname = dr.GetValue(5).ToString();
    if (File.Exists(fname))
    pictureBox1.Image = Image.FromFile(fname);
    }
    con.Close();

    }

    //insert into student values (1,'supriya',55,66,77,'d:abc.jpg')
    // insert into student(RNO,SNAME,m1,m2,m3,photo) values (1,'supriya',55,66,77,'d:abc.jpg')

    private void button2_Click(object sender, EventArgs e)
    {
    try
    {
    qry = "insert into student values(" + int.Parse(textBox1.Text) + "," + "'" + textBox2.Text + "'" + "," + int.Parse(textBox3.Text) + "," + int.Parse(textBox4.Text) + "," + int.Parse(textBox5.Text) + "," + "'" + fname + "'" + ")";

    //MessageBox.Show(qry);
    con.Open();
    cmd.Connection = con;
    cmd.CommandText = qry;
    r = cmd.ExecuteNonQuery();
    if (r > 0)
    MessageBox.Show("Record is added");
    else
    MessageBox.Show("Record is not added");
    }
    catch (Exception e1)
    {
    MessageBox.Show("Error occurred"+e1.ToString());
    }
    con.Close();

    }

    private void button5_Click(object sender, EventArgs e)
    {
    if (openFileDialog1.ShowDialog()==DialogResult.OK)
    {
    fname=openFileDialog1.FileName;
    pictureBox1.Image=Image.FromFile(fname);
    }
    }
    //update student set sname='vaishali' , m1=11 , m2=12 , m3=13 , image='azul.jpg' where rno=12

    private void button4_Click(object sender, EventArgs e)
    {
    qry = "update student set rno="+int.Parse(textBox1.Text)+" , sname=" + "'" + textBox2.Text + "'" + " , m1=" + int.Parse(textBox3.Text) + " , m2=" + int.Parse(textBox4.Text) + " , m3=" + int.Parse(textBox5.Text) + " , photo=" + "'" + fname + "'" + " where rno=" + r1;

    MessageBox.Show(qry);
    con.Open();
    cmd.Connection = con;
    cmd.CommandText = qry;
    r = cmd.ExecuteNonQuery();
    if (r > 0)
    MessageBox.Show("Record is Updated");
    else
    {
    MessageBox.Show("Record is not Updated");

    }
    con.Close();

    }

    // delete from student where rno=67

    private void button3_Click(object sender, EventArgs e)
    {
    qry = "delete from student where rno=" + int.Parse(textBox1.Text);
    MessageBox.Show(qry);
    con.Open();
    cmd.Connection = con;
    cmd.CommandText = qry;
    r = cmd.ExecuteNonQuery();
    if (r > 0)
    MessageBox.Show("Record is Deleted");
    else
    MessageBox.Show("Record is not Deleted");

    con.Close();
    }

    private void button6_Click(object sender, EventArgs e)
    {
    qry = "select * from student";

    con.Open();
    cmd.Connection = con;
    cmd.CommandText = qry;
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
    textBox1.Text = dr.GetValue(0).ToString();
    textBox2.Text = dr.GetValue(1).ToString();
    textBox3.Text = dr.GetValue(2).ToString();
    textBox4.Text = dr.GetValue(3).ToString();
    textBox5.Text = dr.GetValue(4).ToString();
    fname = dr.GetValue(5).ToString();
    if (File.Exists(fname))
    pictureBox1.Image = Image.FromFile(fname);
    MessageBox.Show("Press any key to continue.....");
    }

    con.Close();
    }
    }
    }[/code]

Sign In or Register to comment.