Wednesday, 16 April 2014

Deleting selected data from database in window form


                                                                                                                                             Previous
                                                                                                                                                   Next

Here i will explain how to delete selected data from database, before this i explain how to update or edit data.

Step(1):-Registration form with delete button

Drag and down a button from toolbox and give it name as Delete.
Step(2):-code

Double click on Delete button and write code. here database is same as before shown you in tutorial 6

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");
    }
}
Step(3):-Output

Run your project and enter ID and click on delete button.
                                                                                                                                             Previous
                                                                                                                                                   Next

No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...