0

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();   
    }
5
  • there is a typo in the query Componetcode instaed of Componentcode? And then you are subject to query injection Commented Jun 20, 2012 at 7:00
  • what's the exception? have u debugged your code? Commented Jun 20, 2012 at 7:01
  • error hasn't shown, didn't get what i want exactly Commented Jun 20, 2012 at 7:02
  • i have created the field in database in the name of Componetcode, that is what a reason i have given like that Commented Jun 20, 2012 at 7:03
  • Is there any data in 'tbl_component' in database against selected Mcodeddl? Commented Jun 20, 2012 at 7:05

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. I have forgot to check the Auto post back friend. Thanks again
1

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.

1 Comment

@user1455232, post your part of aspx page containing the dropdownlist
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.