开发者

Error in Update query [Using String.Format]

开发者 https://www.devze.com 2023-04-09 06:31 出处:网络
I am trying to write an update statement for inserting data from asp.net gridview to sql server 2005 database.but it is showing me an error, Please tell me how to solve.

I am trying to write an update statement for inserting data from asp.net gridview to sql server 2005 database.but it is showing me an error, Please tell me how to solve.

cmdUpdate.CommandText = String.Format("Update Products SET ProductName=
{0},UnitsInStock={1},UnitsOnOrder={2},ReorderLevel={3} WHERE ProductID={4} AND 
SupplierID={5}", "productname.Text, unitsinstock.Text, unitsonorder.Text,  
recorderlevel.Text, employeeid.Text, supplierid.Text");

Error开发者_JS百科 is- Index (zero based) must be greater than or equal to zero and less than the size of the argument list.


Your syntax for string.Format is incorrect - each parameter after the string template should be on their own, without the double quotes surrounding them all...

This will work (notice I've removed the double quotes from just before 'productname.Text' and after 'supplierid.Text'):

String.Format("Update Products SET ProductName={0}, UnitsInStock={1}, UnitsOnOrder={2}, ReorderLevel={3} WHERE ProductID={4} AND SupplierID={5}", 
    productname.Text, unitsinstock.Text, unitsonorder.Text, 
    recorderlevel.Text, employeeid.Text, supplierid.Text);


You missed the arguments,

For instance,

str=String.Format("{0} {1}",arg1,arg2);

Do not use hard-coded sql strings. Try to learn/use parameterized queries.

EDIT:

string ConnectionString = "put_connection_string";
using (SqlConnection con = new SqlConnection(ConnectionString))
{
    using (SqlCommand cmd = new SqlCommand())
    {
        string sql = "Update Products SET 
                ProductName=@ProductName,
                UnitsInStock=@UnitsInStock,
                UnitsOnOrder=@UnitsOnOrder,
                ReorderLevel=ReorderLevel 
                WHERE ProductID=ProductID AND SupplierID=@SupplierID";
        cmd.CommandText = sql;
        cmd.Connection = con;
        cmd.Parameters.Add("@ProductName", System.Data.SqlDbType.VarChar, 50).Value =productname.Text;
        cmd.Parameters.Add("@UnitsInStock", System.Data.SqlDbType.Int).Value =unitsinstock.Text;
        cmd.Parameters.Add("@UnitsOnOrder", System.Data.SqlDbType.Int).Value =unitsonorder.Text;
        cmd.Parameters.Add("@ReorderLevel ", System.Data.SqlDbType.Int).Value =recorderlevel.Text;
        cmd.Parameters.Add("@ProductID", System.Data.SqlDbType.Int).Value =producteid.Text;
        cmd.Parameters.Add("@SupplierID", System.Data.SqlDbType.Int).Value =supplierid.Text;

        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

EDIT: What is C# using block?

  1. If the type implements IDisposable, it automatically disposes it
  2. Provides a convenient syntax that ensures the correct use of IDisposable objects.
  3. Avoiding Problems with the Using Statement
0

精彩评论

暂无评论...
验证码 换一张
取 消