2 Replies - 2700 Views - Last Post: 18 February 2009 - 01:46 PM Rate Topic: -----

#1 jl938206  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-February 09

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?

 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();
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Updating database with drop down list

#2 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

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:

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
Was This Post Helpful? 0
  • +
  • -

#3 avolel  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 42
  • Joined: 24-August 05

Re: Updating database with drop down list

Posted 18 February 2009 - 01:46 PM

View Postjl938206, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1