0

I am usng VS 2010 and I have a Website I am building using C#.
My goal is when a user selects a value from the dropdownlist it is recorded in the database.

I have been able to bind the DDL to a table called "TableA" to list the values needed sucessfully.

But, I want the values to be recorded in a table called "tbl_Main"

The DDL ID on the the aspx page is "ut_1" and the column name in the table that I want the data recorded to is called "ut_1" aswell.

No errors show when debugging but the values never update.

Any help would be greatly appreciated. :)

Here is my BehindCode so far.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace techtrax
{ 
    public partial class Default : System.Web.UI.Page
    {
        protected void ddl_ut1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["techtraxConnectionString"].ConnectionString);
             sqlConn.Open();
                SqlCommand sqlCmd = new SqlCommand("Update tbl_main SET ut_1=@ut_1 where JSN=@JSN", sqlConn);
            DropDownList ddl = (DropDownList)sender;    
            sqlCmd.Parameters.Add("@ut_1", System.Data.SqlDbType.NVarChar, 50);
            sqlCmd.Parameters["@ut_1"]. = ddl.SelectedItem.Value;
              sqlConn.Close();
           
        }

    }
}
3
Contributors
5
Replies
12
Views
7 Years
Discussion Span
Last Post by stardust16
0

On line 20 it looks like you've copied/pasted a bit weirdly...
I think it should be:

sqlCmd.Parameters["@ut_1"].Value = ddl.SelectedItem.Value;
0

Thank you I did correct the line.

However It still not working. "Not updating the database" :)

And I am not sure how to handle this error.

The Same line is giving this error:

Cannot apply indexing with [] to an expression of type 'method group'

namespace techtrax
{ 
    public partial class Default : System.Web.UI.Page
    {
        protected void ddl_ut1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["techtraxConnectionString"].ConnectionString);
             sqlConn.Open();
                SqlCommand sqlCmd = new SqlCommand("Update tbl_main SET ut_1=@ut_1 where id=@id", sqlConn);
                DropDownList ddl = (DropDownList)sender;    
                sqlCmd.Parameters.Add("@ut_1", System.Data.SqlDbType.NVarChar, 50);
                sqlCmd.Parameters.Add["@ut_1"].Value = ddl.SelectedItem.Value;
            
            sqlConn.Close();
           
        }

    }
}
0

You haven't corrected it quite right.
Line 11 creates the parameter, and defines the type of data it will hold.
Line 12 needs to set the value of that parameter. The parameter already exists, so it doesn't need the ".Add" again. It's exactly as I posted previously.

For the Update to work, you will also need to create, and set the value of the "id" parameter.


Alternatively, if you trust it to figure out the correct types by itself, then you could use something like this instead:

sqlCmd.Parameters.AddWithValue("ut_1", ddl.SelectedItem.Value);
sqlCmd.Parameters.AddWithValue("id", WhateverTheIdIs);

Edited by mikev2: n/a

0

Mike,

Thanks,
I see what you were saying my two lines we corrected to this:

sqlCmd.Parameters.Add("@ut_1", System.Data.SqlDbType.NVarChar, 50);
        sqlCmd.Parameters["@ut_1"].Value = ddl.SelectedItem.Value;

Also I found I need to pay more attention as I work :) I added the ut_1 column to my table but I did not refresh my datasource on the webpage. Once I updated my datacource on the page and corrected the two lines everything worked.

Thanks again Problem solved.

0

I am trying to do same thing with a drop down list except that I don`t know how to set the variable for id, in your case JSN=@JSN (the WHERE clause from the update command)
did you used session variables or just got it from a textbox?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.