user1455232
user1455232

Reputation: 43

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

Upvotes: 0

Views: 101

Answers (3)

uowzd01
uowzd01

Reputation: 1000

You should set a breakpoint inside your Mcodeddl_SelectedIndexChanged method to see if the event is triggered, also make sure include AutoPostBack="true" in your dropdownlist definition

Upvotes: 2

user1455232
user1455232

Reputation: 43

i have got the answer. And i gave the detail what exactly i have did. "Set AutoPostBack=True"

Upvotes: 1

Habib
Habib

Reputation: 223257

Make sure you have specified OnSelectedIndexChanged event for the drop down in aspx page

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

Also use parameterized SQL queries.

PS. Your SO(); is a method not a class.

Upvotes: 1

Related Questions