c# - Cannot Save Data in my database using a DataGridView -
i have made application can add,view,update , delete data database using datagridview. have made separate forms each operation using dgv. in update form i'm not able update or edit data dgv , data not stored in database dgv. please tell me correct code. code
public update() { initializecomponent(); } sqlconnection sql_con; sqlcommand sql_cmd; sqldataadapter sql_ad; sqlcommandbuilder sql_cb; datatable dt; dataset ds; string connect_string; private void update_load(object sender, eventargs e) { datagridview1.refresh(); connect_string = @"data source=.\sqlexpress;attachdbfilename=c:\users\administrator\desktop\form\form\employeedb.mdf;integrated security=true;user instance=true"; // todo: line of code loads data 'ds_employeedb.emp_info' table. can move, or remove it, needed. this.emp_infotableadapter.fill(this.ds_employeedb.emp_info); string select_cmd = "select * emp_info"; sql_con = new sqlconnection(connect_string); sql_con.open(); sql_cmd = new sqlcommand(select_cmd,sql_con); sql_ad = new sqldataadapter(sql_cmd); sql_cb = new sqlcommandbuilder(sql_ad); ds = new dataset(); sql_ad.fill(ds,"emp_info"); dt=ds.tables["emp_info"]; sql_con.close(); datagridview1.datasource = dt; } private void button_search_click(object sender, eventargs e) { string select_cmd = "select * emp_info f_name='" + textbox_search.text + "'"; sql_con = new sqlconnection(connect_string); dt = new datatable(); sql_ad = new sqldataadapter(select_cmd, sql_con); sql_ad.fill(dt); datagridview1.datasource = dt; } private void datagridview1_cellendedit(object sender, datagridviewcelleventargs e) { datagridview1.refresh(); } private void button_update_click(object sender, eventargs e) { string update_query = "update emp_info set " + "f_name = '" + datagridview1.selectedrows[0].cells[1].value.tostring() + "' ," + "l_name = '" + datagridview1.selectedrows[0].cells[2].value.tostring() + "' ," + "father_name = '" + datagridview1.selectedrows[0].cells[3].value.tostring() + "' ," + "education = '" + datagridview1.selectedrows[0].cells[4].value.tostring() + "' ," + "institute = '" + datagridview1.selectedrows[0].cells[5].value.tostring() + "' ," + "address = '" + datagridview1.selectedrows[0].cells[6].value.tostring() + "' ," + "phone = '" + datagridview1.selectedrows[0].cells[7].value.tostring() + "' ," + "cinc = '" + datagridview1.selectedrows[0].cells[8].value.tostring() + "' ," + "where '" + textbox_search.text + "'"; sql_con = new sqlconnection(connect_string); sql_con.open(); sql_cmd = new sqlcommand(update_query, sql_con); sql_ad = new sqldataadapter(sql_cmd); sql_cb = new sqlcommandbuilder(sql_ad); ds = new dataset(); sql_ad.fill(ds, "emp_info"); dt = ds.tables["emp_info"]; sql_con.close(); datagridview1.datasource = dt; } private void button_cancle_click(object sender, eventargs e) { close(); } }
}
Comments
Post a Comment