简体   繁体   English

从数据库错误检索C#数据

[英]C# data retrieve from database error

i have tried to to load the data to text box, while selecting the drop down list, i was created a class for retrieval and called it in drop down list selected index changed. 我试图将数据加载到文本框中,同时选择下拉列表,我创建了一个用于检索的类,并在下拉列表中将其称为选定索引已更改。 But i cant get the answer what i want. 但是我无法得到我想要的答案。 If i have called the class in Button click event it has worked properly. 如果我在Button click事件中调用了该类,则该类工作正常。 So please correct me. 所以请纠正我。 What i made a mistake. 我做错了什么。 This is what my code: 这是我的代码:

public void so()
    {
        con.Open();
        string s2;
        s2 = "select Source from tbl_component where Componetcode='" + Mcodeddl.SelectedItem.Text + "'";
        SqlCommand c2 = new SqlCommand(s2, con);
        SqlDataReader d2;
        d2 = c2.ExecuteReader();
        while (d2.Read())
        {
            TextBox1.Text = d2["Source"].ToString().Trim(); 

        }
        d2.Close();
        con.Close();
    }

//i have called the so class here

 protected void Mcodeddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        so();   
    }

您应该在Mcodeddl_SelectedIndexChanged方法内设置一个断点,以查看是否触发了该事件,并确保在下拉列表定义中包含AutoPostBack="true"

Make sure you have specified OnSelectedIndexChanged event for the drop down in aspx page 确保已为aspx页面中的下拉列表指定OnSelectedIndexChanged事件

<asp:DropDownList ID="Mcodeddl" runat="server" 
    OnSelectedIndexChanged= "Mcodeddl_SelectedIndexChanged">
</asp:DropDownList>

Also use parameterized SQL queries. 还可以使用参数化的 SQL查询。

PS. PS。 Your SO(); 您的SO(); is a method not a class. 是一个方法而不是一个类。

i have got the answer. 我有答案。 And i gave the detail what exactly i have did. 我详细说明了我的所作所为。 "Set AutoPostBack=True" “设置AutoPostBack = True”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM