1

i have a SQL query as follows

  String S = Editor1.Content.ToString();
     Response.Write(S);   
    string sql = "insert into testcase.ishan(nmae,orders) VALUES ('9',@S)";
   OdbcCommand cmd = new OdbcCommand(sql, myConn);
            cmd.Parameters.AddWithValue("@S", S); 
            cmd.ExecuteNonQuery();

Error: Column 'orders' cannot be null at System.Data.Odbc.OdbcConnection.HandleError

1 Answer 1

3

Better to use something like:

var sqlCommandText = "insert into testcase.ishan(nmae,orders) VALUES ('9', @S)";
using (var connection = new SqlConnection())
{
    using (var command = new SqlCommand(sqlCommandText , connection))
    {
        command.Parameters.AddWithValue("@S", S);
        command.ExecuteNonQuery();
    }
}

The use of parameters prevents SQL injection. Andrews solution does not, I believe.

ExecuteNonQuery() will return the number of rows affected, if you need it.

The using statements will take care of disposing the connection properly, so afterwards, you have no open connections anymore. This is because SqlConnection implements IDisposable.

Sign up to request clarification or add additional context in comments.

6 Comments

The type or namespace name 'SqlConnection' could not be found
add this using: using System.Data.SqlClient;
You should reference/import/using System.Data.SqlClient. See: msdn.microsoft.com/en-us/library/…
i am getting this error Column 'orders' cannot be null at System.Data.Odbc.OdbcConnection.HandleError
@user486050: have you ever set any value for S?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.