protected void btnConfirm_Click(object sender, EventArgs e)
{
String Ourdatabase = ConfigurationManager.ConnectionStrings["OurDatabase"].ToString();
SqlConnection OurDatabase = new SqlConnection("OurDatabase");
OurDatabase.Open();
SqlCommand UpdateCommand = new SqlCommand(
"Update LineItem SET [Status]=" + Convert.ToString(ddlStatus.SelectedValue)+ "WHERE LineItemID = @LineItemID",
OurDatabase);
UpdateCommand.Parameters.Add(ddlStatus.SelectedValue, SqlDbType.NVarChar).Value = ddlStatus.SelectedValue;
UpdateCommand.ExecuteNonQuery();
OurDatabase.Close();
}
Updating database with drop down listHaving issues with update
Page 1 of 1
2 Replies - 2090 Views - Last Post: 18 February 2009 - 01:46 PM
#1
Updating database with drop down list
Posted 11 February 2009 - 07:44 PM
I'm trying to update a database using a drop down list. I've tried many things but they've all failed, can someone please help?
Replies To: Updating database with drop down list
#2
Re: Updating database with drop down list
Posted 12 February 2009 - 01:10 AM
Hi!
I m not an expert in SQL databases, and i m not at home so i cannot check it, BUT:
You declared Ourdatabase twice...
Maybe change to SqlConnecten OurDB instead of using OurDatabase again..
Hope that helps!
B.Ihde
I m not an expert in SQL databases, and i m not at home so i cannot check it, BUT:
String Ourdatabase = ConfigurationManager.ConnectionStrings["OurDatabase"].ToString();
SqlConnection OurDatabase = new SqlConnection("OurDatabase");
You declared Ourdatabase twice...
Maybe change to SqlConnecten OurDB instead of using OurDatabase again..
Hope that helps!
B.Ihde
#3
Re: Updating database with drop down list
Posted 18 February 2009 - 01:46 PM
jl938206, on 11 Feb, 2009 - 06:44 PM, said:
I'm trying to update a database using a drop down list. I've tried many things but they've all failed, can someone please help?
protected void btnConfirm_Click(object sender, EventArgs e)
{
String Ourdatabase = ConfigurationManager.ConnectionStrings["OurDatabase"].ToString();
SqlConnection OurDatabase = new SqlConnection("OurDatabase");
OurDatabase.Open();
SqlCommand UpdateCommand = new SqlCommand(
"Update LineItem SET [Status]=" + Convert.ToString(ddlStatus.SelectedValue)+ "WHERE LineItemID = @LineItemID",
OurDatabase);
UpdateCommand.Parameters.Add(ddlStatus.SelectedValue, SqlDbType.NVarChar).Value = ddlStatus.SelectedValue;
UpdateCommand.ExecuteNonQuery();
OurDatabase.Close();
}
Try taking out ' UpdateCommand.Parameters.Add(ddlStatus.SelectedValue, SqlDbType.NVarChar).Value = ddlStatus.SelectedValue;'
you don't need to convert the selected value to string. The SelectedValue property already returns the selected value as a string.
PS:
You should make a connection to the DB like:
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
try
{
cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = "Ur Update statement goes here";
cmd.Parameters.AddWithValue("@LineItemId", LineItemId);
conn.Open();
cmd.ExecuteNonQuery()
conn.Close();
}
}
catch (Exception ex)
{
throw;
}
finally
{
if (cmd != null)
{
cmd.Dispose();
file.Close();
}
}
}
Helps prevents SQL Injection and handles any errors your sql throws.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|