I am using Visual Studio 2013 and SQL Server 2012. I want to save my data in the database but it gives:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
My code:
private void button1_Click(object sender, EventArgs e)
{
string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand("ups_StudentInsertDetails"))
{
cmd.Parameters.AddWithValue("Name", textBox1.Text);
cmd.Parameters.AddWithValue("Email", textBox2.Text);
//then open connection
conn.Open();
//Execute Reader(select ststement)
//Execute Scalar(select ststement)
//Executenonquery (Insert , update or delete)
cmd.ExecuteNonQuery();
MessageBox.Show("Data saved successfully!");
}
}
}
}
Open your sqlconnection like this and associate it with the sqlcommand. See the code changes below highlighted in comments.
}