Code for Submit, Update , Delete in Window Form C#

This is code for submit, update, delete in Window Form C#.

Code For Submit

private void Submit_txt_Click(object sender, EventArgs e)

{

    string str = "server = MUNESH\\SQL2008R2;Database=datastore;UID=sa;Password=123;";

    SqlConnection con = new SqlConnection(str);

    string query = "insert into table(ID,Name,Father) values('" + this.id_txt.Text + "','" + this.textBox1.Text + "','" + this.textBox2.Text +"')";

    SqlCommand cmd = new SqlCommand(query, con);

    SqlDataReader dbr;

    try

    {

        con.Open();

        dbr = cmd.ExecuteReader();

       // MessageBox.Show("save");

       MessageBox.Show("Records saved successfully", "user information");

       while (dbr.Read())

       {

        }

        textBox2.Text = string.Empty;

       textBox3.Text = string.Empty;

       id_txt.Text = string.Empty;

       con.Close();

} 

Code For Update

private void Update_txt_Click(object sender, EventArgs e)

{

    string str = "server = MUNESH\\SQL2008R2;Database=datastore;UID=sa;Password=123;";

    SqlConnection con = new SqlConnection(str);

    string query = "UPDATE data1 set Name = '" + this.textBox1.Text + "', Father = '" + this.textBox2.Text + "' where ID= '" + this.id_txt.Text + "' ";

    SqlCommand cmd = new SqlCommand(query, con);

    SqlDataReader myreader;

    try

   {

       con.Open();

       myreader = cmd.ExecuteReader();

       MessageBox.Show(" successfully record Updated", "user information");

       while (myreader.Read())

       {

       }

      con.Close()

    }

    catch (Exception ec)

    {

        MessageBox.Show(ec.Message);

    }

} 

Code For Delete

private void Delete_txt_Click(object sender, EventArgs e)

{

    if (!(id_txt.Text == string.Empty))

    {

        string str = "server = MUNESH\\SQL2008R2;Database=datastore;UID=sa;Password=reladmin@123;";

        SqlConnection con = new SqlConnection(str);

        string query = "Delete from data1 where ID= '" + this.id_txt.Text + "'";

        SqlCommand cmd = new SqlCommand(query, con);

        SqlDataReader myreader;

        try

        {

             con.Open();

            myreader = cmd.ExecuteReader();

            MessageBox.Show("successfully data Deleted","user information");

            while (myreader.Read())

           {

           }

           con.Close();

       }

      catch (Exception ec)

      {

         MessageBox.Show(ec.Message);

      }

    }

    else

   {

        MessageBox.Show("enter ID which you want to delete","User information");

    }

}

Ebook Download
View all
Learn
View all