.net - C# SQL query exception -


I am using C # in .NET 2.0 and I am trying to access and manipulate a database . As I wish I can read from DB several times and everything works, but as soon as I try to insert an item I get the following error message:

 < Code> ExecuteNonQuery requires an open and available connection The current status of the connection is closed.  

I tried to see it, but I was able to find the fix or did not work either or not.

I have the following code:

 using  (SqlConnection Conn = New SqlConnection (SQLConnectionString)) {SqlDataAdapter dataAdapter = New SqlDataAdapter ("Select [Col1] from [ Table1] where [Col2] = '' + val2 + "'', conn); Dataset dataset = new dataset (); DataAdapter.Fill (dataset); (DataSet.Tables.Count == 0 || dataSet.Tables [0] .Rows.Count! = 1) {SqlCommand cmd = New SqlCommand ("In [Table1] ([Col1], [Col2]) Enter Value if ( '"+ Val1 +"', '"+ 2 + 2") ", Kon); Cmd.ExecuteNonQuery (); }}  


Note: I'm sure I have a properly set up permission because the Visual Studio can be included with the same SQLConnectionString. Besides, I'm still quite new to the database, so if I'm doing something blantently wrong, please let me know.

Thank you.

You do not appear to be opening the connection to perform your update (exception So you tell this) .

Try this

  SqlCommand cmd = Enter the new SqlCommand value ("Table1" ([Col1], [Col2]) ('' + VAL1 + '' ',' '+ Val2 +' ') ", conn); Conn.Open (); Cmd.ExecuteNonQuery (); Conn.Close (); You can do more details and work on MSDN at  

You can


Comments