开发者

Database to DropDownList and AutoPostBack to Label

开发者 https://www.devze.com 2023-02-06 08:52 出处:网络
I have a code like this protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) {

I have a code like this

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;

                SqlConnection myConnection = new SqlConnection(strConnectionString);
                myConnection.Open();
string musisim = DropDownList1.SelectedItem.Value;

                SqlCommand cmd = new SqlCommand("select B.HESAP_NO FROM  YAZ..MARDATA.S_TEKLIF B WHERE B.MUS_K_ISIM = DropDownList1.SelectedItem.Value", myConnection);

                Label1.Text = cmd.ExecuteReader().ToString();

                myConnection.Close();

i have a customer name as "MUS_K_ISIM" and his number as "HESAP_NO"

All i want is, (autopostback is true) automaticly getting label "HESAP_NO" with who has this number "MUS_K_ISIM" in Dropdownlist.

How can i do that?

开发者_C百科

Error: 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.


Your database doesn't know anything about your ASP page or controls on it ;-) so you'll need to re-work your SQL statement - As a first revision you could change it thus:

SqlCommand cmd = new SqlCommand("select B.HESAP_NO FROM  YAZ..MARDATA.S_TEKLIF B WHERE B.MUS_K_ISIM = '" + DropDownList1.SelectedItem.Value + "'", myConnection);

so that the name selected in your dropdownlist will be added to the SQL query.

Looking ahead, its best to do this kind of thing with either a Parameterized Query or a Stored procedure.

HTH.


Hi just use a parametrized query like this

Also there is likely a typo in table name YAZ..MARDATA.S_TEKLIF in query it might be 'YAZ.MARDATA.S_TEKLIF'

Updated code

string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;

        using (SqlConnection myConnection = new SqlConnection(strConnectionString))
        {
            string query = "select B.HESAP_NO FROM  YAZ.MARDATA.S_TEKLIF B WHERE B.MUS_K_ISIM = @selectedItem";
            using (SqlCommand cmd = new SqlCommand(query, myConnection))
            {
                cmd.Parameters.AddWithValue("@selectedItem", DropDownList1.SelectedValue.ToString());
                myConnection.Open();
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.HasRows)
                    {
                        Label1.Text = dr["B.HESAP_NO"].ToString();
                    }
                    dr.Close();
                }
            }
            myConnection.Close();
        }


SqlCommand cmd = new SqlCommand("select B.HESAP_NO FROM  YAZ..MARDATA.S_TEKLIF B WHERE B.MUS_K_ISIM = DropDownList1.SelectedItem.Value", myConnection);

Are you hardcoding the text ?, You need to pass the selected value i presume, the one in

musisim


There is no need to connect to the DB with code. You can do all this with one line of code.

Add a SqlDataSource and configure it with the SQL command you need.

Connect the DropDown to the SqlDataSource, configure what column should be connected to the Text Field and what column should be connected to the Value field.

In your function DropDownList1_SelectedIndexChanged

Just do this:

 Label1.Text = DropDownList1.SelectedValue();
0

精彩评论

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