private void btnSave_Click(object sender, EventArgs e) { using(AdoClassLibrary.DataBaseLayer dbLayer = new AdoClassLibrary.DataBaseLayer()) { string cmdStr = "[dbo].[SaveContacts]"; SqlCommand cmd = new SqlCommand(cmdStr); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@firstName", SqlDbType.NVarChar).Value = txtFstname.Text; cmd.Parameters.Add("@lastName", SqlDbType.NVarChar).Value = txtLstName.Text; cmd.Parameters.Add("@mobileNumber", SqlDbType.NVarChar).Value = txtMobile.Text; cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = txtEmail.Text; try { int result = dbLayer.ExecuteNonQuery(cmd); if (result > 0) { MessageBox.Show("Successfully Inserted"); loadData(); } else { MessageBox.Show("Failed to Insert Contact"); } } catch(Exception ex) { MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public void loadData() { using (AdoClassLibrary.DataBaseLayer dbLayer = new AdoClassLibrary.DataBaseLayer(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString)) { string cmdStr = "[dbo].[GetContacts]"; SqlCommand cmd = new SqlCommand(cmdStr); cmd.CommandType = CommandType.StoredProcedure; try { DataSet dataSet = dbLayer.FillDataSet(cmd); contactsGrid.DataSource = dataSet.Tables[0]; } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }