Whenever update the values in the text box.. its not submitted and update in database.
Thanks and regards

Here is my code :


namespace sys.System
{
public partial class spareedit : System.Web.UI.Page
{
SqlConnection dbCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["sqlServer2"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
dbCon.Open();
SqlCommand cmd = new SqlCommand("SELECT [Category],[Part Number],[Description],[Contract],[Current Quantity],[Site] FROM [demo].[dbo].[Sparepartslist] where [Part Number] = '" + Request.QueryString["id"] + "' and [site] = '" + Session["site"] + "'", dbCon);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
lblDescription.Text = dr["Description"].ToString();
lblpartnumber.Text = dr["Part Number"].ToString();
lblContract.Text = dr["Contract"].ToString();
txtQuantity.Text = dr["Current Quantity"].ToString();

}
dr.Close();


dbCon.Close();
}
}
protected void txtQuantity_TextChanged(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
string str = txtQuantity.Text;

SqlCommand com;

dbCon.Open();
com = new SqlCommand("Update into Sparepartslist values(@Current Quantity)", dbCon);
com.Parameters.AddWithValue("@Current Quantity", txtQuantity.Text);
com.ExecuteNonQuery();
com.Dispose();
dbCon.Close();
Response.Write("Records successfully updated");
clear();
}
void clear()
{
txtQuantity.Text = "";
}

}


}

use [ code ] [ /code ] tags (without the spaces. It's hard to read that mess.

How are you running this? From inside Visual Studio? What database are you using? Are you running in Debug mode?

You do realize that your update statement will affect every record in the table, since you didn't qualify which records with a where statement?

Edited 4 Years Ago by Momerath: n/a

the problem at


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'nvarchar'.

Source Error:


Line 57: com = new SqlCommand("Update into Sparepartslist values(@Current Quantity)", dbCon);
Line 58: com.Parameters.AddWithValue("@Current Quantity", txtQuantity.Text);
Line 59: com.ExecuteNonQuery();
Line 60: com.Dispose();
Line 61: dbCon.Close();

Update into??
This is no sql query.
OR: SELECT a from table
OR: INSERT INTO table VALUES(valueA, ..)
OR: UPDATE table SET valueA = @param1,...

So whats it gonna be?

If you mean to do an UPDATE you do:

string query = @"Update Sparepartslist SET ColumnName2 = @param1 WHERE ColumnName1 = @param2"; //NO WHITE SPACES, like you did!!!
//AND WHERE clause SI NEEDED so the code knows which row to update!! usually its an ID column, or some uniqe name!
com = new SqlCommand(query, dbCon);
com.Parameters.Add("@param2", SqlDbType.Int).Value = "your unique value, like ID";
com.Parameters.Add("@param1", SqlDbType.Int).Value = "your value to update"; //chnage tge Type to what the column really is a type of!
com.ExecuteNonQuery();

Edited 4 Years Ago by Mitja Bonca: n/a

This question has already been answered. Start a new discussion instead.