Error While Using Executenonquery C#

Error while using ExecuteNonQuery c#

I had a similar issue. I have an SMO application which uses the Microsoft.SqlServer.SqlManagementObjects nuget package.

The app all runs perfectly on my p.c however when it came to deploying it and running it on our app server i was getting the error.

Could not load file or assembly 'Microsoft.SqlServer.BatchParser.dll' or one of its dependencies. The specified module could not be found.

To fix the issue I needed to build the project again targeting x86 platform.

Error while executing ExecuteNonQuery() function in c#

You might wanna try:

insert into emp (fname,minit,lname,dbate,ssn,sex) values ('arun', NULL, NULL, NULL,'12345', NULL)

Check if the user is typing some characters like ' and -- it can easily corrupt your syntax!!!

OFF-TOPIC BUT RELATED: By getting your values directly from the textbox youre allowing anyone to inject SQL on your application, its therefore a flaw. You can avoid it by parametrizing your query, such as follow:

SqlCommand cmd = new SqlCommand("select * where Pk = @param1");
cmd.Parameters.Add("@param1", SqlDbType.Int);
cmd.Parameters["@param1"].Value = txtPk.text;

Error in ExecuteNonQuery()

yes i have done it. The convert to double thing works 
thankyou all
Appriciated.[enter link description here][1]

cmd.Parameters.AddWithValue("@txt_rdvalue",txt_rdvalue.Text);
cmd.Parameters.AddWithValue("@txt_orderid",Convert.ToDouble(txt_orderid.Text));
cmd.Parameters.AddWithValue("@cb_oname", cb_oname.SelectedText);
cmd.Parameters.AddWithValue("@cb_ocat", cb_ocat.SelectedText);
cmd.Parameters.AddWithValue("@cb_oqty", Convert.ToDouble(cb_oqty.SelectedValue));
cmd.Parameters.AddWithValue("@txt_oprice",Convert.ToDouble((txt_oprice.Text)));
cmd.Parameters.AddWithValue("@txt_disc",Convert.ToDouble(txt_disc.Text));
cmd.Parameters.Add(new SqlParameter("@Date", dateTimePicker1.Value.Date));

[1]: http://www.stackoverflow.com/alygorejaanswers

Why does cmd.ExecuteNonQuery(); give me error near insert into Table...

Table is a reserved word for T-SQL. You need to write it between [ ] in case it is really the name of your table.

Like this.

insert into [Table] values ...

https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql?view=sql-server-2017

Update with parameters for the second problem (incorrect number of columns…)

How do I to insert data into an SQL table using C# as well as implement an upload function?

SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO [Table] (ColumnName1, ColumnName2, ColumnName3, ColumnName4, ColumnName5) VALUES (@ColumnName1, @ColumnName2, @ColumnName3, @ColumnName4, @ColumnName5)";

cmd.Parameters.AddWithValue("@ColumnName1", textBox1.Text);
cmd.Parameters.AddWithValue("@ColumnName2", textBox2.Text);
cmd.Parameters.AddWithValue("@ColumnName3", textBox3.Text);
cmd.Parameters.AddWithValue("@ColumnName4", textBox4.Text);
cmd.Parameters.AddWithValue("@ColumnName5", textBox5.Text);

cmd.ExecuteNonQuery();

con.Close();

Just replace ColumnName1 ColumnName2... with the name of your columns and textBox1.Text, textBox2.Text... with your input textboxes

Don't forget to cast the textboxX.Text to the desired datatype if the column doesn't match with string type.

https://learn.microsoft.com/es-es/dotnet/api/system.int32.parse?view=netframework-4.7.2

cmd.Parameters.AddWithValue("@ColumnName4", int.Parse(textBox4.Text));
cmd.Parameters.AddWithValue("@ColumnName5", int.Parse(textBox5.Text));

SQL Update command error in ExecuteNonQuery

There are a couple of problems with your code

  1. The syntax for UPDATE is incorrect. It should be UPDATE SET columnName = value...
  2. Use parameterised queries, because at the moment your code is vulnerable to SQL injection
  3. Move myCommand.ExecuteNonQuery(); inside the try block to catch any exceptions

Please see my update to your code:

var sqlstring = @"UPDATE Email SET Email = @email, Description = @description, UserName = @username, Title = @title WHERE ID = @id");

var myConnection = getconection();
SqlCommand myCommand = new SqlCommand(sqlstring, myConnection);

// add parameters
myCommand.Parameters.AddWithValue("@email", email);
myCommand.Parameters.AddWithValue("@description", description);
myCommand.Parameters.AddWithValue("@username", userName);
myCommand.Parameters.AddWithValue("@title", title);
myCommand.Parameters.AddWithValue("@id", emailId);

try
{
// execute the command in the try block to catch any exceptions
myCommand.ExecuteNonQuery();
myConnection.Close();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

As mentioned in the comments, you should really perform the update in the HttpPost method and validate the values before calling UpdateDataBase().

Error ExecuteNonQuery: Connection property has not been initialized C# (Access)

You use disposed (closed with all resources released) connection:

private OleDbConnection openConnection()
{
// you create the connection
using var conn= new OleDbConnection(connString);
conn.Open();
return conn;
} // <- and here you dispose it

The very same problem with Command: the cmd you return is disposed.

private OleDbCommand getCommand(string queryString, OleDbConnection conn)
{
using var cmd = new OleDbCommand(queryString, conn); // <- command created
return cmd;
} // <- and disposed

You can drop using in both methods:

private OleDbConnection openConnection()
{
// we create connection
conn = new OleDbConnection(connString);

try {
// try it to open
conn.Open();
// return opened (and not disposed!) connection
return conn;
}
catch {
// on open failure we dispose conenction (to prevent resources leakage)
conn.Dispose();
// and rethrow the exception (to know the exact problem)
throw;
}
}

// Here we just return a command
private OleDbCommand getCommand(string queryString, OleDbConnection conn) =>
new OleDbCommand(queryString, conn);

Or may be merge methods into one:

public int Command(string queryCommand) 
{
using var conn = new OleDbConnection(connString);
conn.Open();

using var cmd = new OleDbCommand(queryString, conn);

return cmd.ExecuteNonQuery();
} // here both conn and cmd will be disposed


Related Topics



Leave a reply



Submit