I’m trying to make A winform CRUD operations app using SQL server but, when running program and perform query, the program said
ConnectionString is not initialized
Even I’ve added the new connection to the existing database and copy the exact the connection string.
I have a class like
public partial class PersonContactForm : Form { // I declared my connect db here SqlConnection sqlCon = new SqlConnection(@"Data Source=DESKTOP-JIV4OD1SQLEXPRESS;Initial Catalog=DapperCrud; Integrated // some code // my test db connect. It does work private void btnTestSqlConn_Click(object sender, EventArgs e) { using (sqlCon = new SqlConnection(@"Data Source=DESKTOP-JIV4OD1SQLEXPRESS;Initial Catalog=DapperCrud; Integrated Security=True;")) { try { sqlCon.Open(); } catch(SqlException sqlEx) { MessageBox.Show(sqlEx.Message); } MessageBox.Show("Connection to SQL OK!"); return; } } // this is a save database. When I clicked on this button. // the error ConnectionString is not initialized occurred. private void btnSave_Click(object sender, EventArgs e) { try { // if sql state is closed so open it if (sqlCon.State == ConnectionState.Closed) sqlCon.Open(); DynamicParameters param = new DynamicParameters(); param.Add("@contactId", contactId); param.Add("@firstName", txbFirstName.Text.Trim()); param.Add("@lastName", txbLastName.Text.Trim()); param.Add("@mobile", txbMobile.Text.Trim()); param.Add("@Address", txbAddress.Text.Trim()); sqlCon.Execute("ContactAddOrEdit", param, commandType: CommandType.StoredProcedure); // if contactId == 0 then we saved data succesfully if (contactId == 0) MessageBox.Show("Saved OK!"); else MessageBox.Show("Updated OK!"); // fill into DataGridView fillDataGridView(); // clear textBox fields after filled clear(); } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { // close sql after executed sqlCon.Close(); } } // .... some code
Anonymous Asked question May 14, 2021
Recent Comments