Krazy Dev Krazy Dev - 6 months ago 9
SQL Question

Store ID from a dropdown list

I have been working on this for a while but seems I can't figure this out. So I have a dropdown list called ddStudent where the textField value is from a sql table I made called 'Student from a column called 'firstName'. Now I used the 'studentID' as the value field.

Here is where I am having the issue. I want to execute my stored procedure that takes studentID as a paramter but I don't understand how to store the actual student ID into the parameter.

Here is what I have so far-

protected void btnRegister_Click(object sender, EventArgs e)
{
int selValClass = Convert.ToInt32(ddClassNames.SelectedValue);
int selValStudent = Convert.ToInt32(ddStudents.SelectedValue);

using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("procRegStudent", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@studentID", selValStudent);
cmd.Parameters.AddWithValue("@classID", selValClass);

con.Open();
cmd.ExecuteNonQuery();
con.Close();

}


and here is my code for databinding-

using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("SELECT studentID, firstName, lastName, ssn, address, city, state, zip FROM Student ", con);

con.Open();

ddStudents.DataSource = cmd.ExecuteReader();
ddStudents.DataTextField = "firstName";
ddStudents.DataValueField = "studentID";
ddStudents.DataBind();

}


Now my question is, is there a way I can store the actual studentID from the dropdown list when I select it? For some reason when I do this, the page reloads and selects the first student ID for a student I didn't select. I'm not exactly sure what's going on.

Thanks.

EDIT-

I wanted to make sure I clarify-

I have 3 students in my student dropdown. When I select student 3 (with student ID 3), the first student in the dropdownlist is being passed as the parameter and I can't tell why.

Answer

This issue is logical for Dropdownlist control because your dropdownlist missed to have a defaultitem to render when it comes back from the server.The soultion is very simple you should add a default list item to be the default value when the page loads

Here I made a simple example to how can we solve this issue :

in the Page.aspx:

    <asp:DropDownList ID="ddStudents" runat="server" DataTextField = "firstName" DataValueField = "studentID" AppendDataBoundItems="true">
        <asp:ListItem Text="===Select Student===" Value="0"></asp:ListItem>

    </asp:DropDownList> 

    <asp:Button ID="btnRegister" runat="server" Text="Register" OnClick="btnRegister_Click" />
    <asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>

in the Page.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDown();

        }

    }

    private void BindDropDown()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("studentID", typeof(int));
        dt.Columns.Add("firstName", typeof(string));

        dt.Rows.Add(dt.NewRow());
        dt.Rows[0]["studentID"] = 1;
        dt.Rows[0]["firstName"] = "Markus";


        dt.Rows.Add(dt.NewRow());
        dt.Rows[1]["studentID"] = 2;
        dt.Rows[1]["firstName"] = "Arthur";


        ddStudents.DataSource = dt;
        ddStudents.DataBind();
    }

    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (ddStudents.SelectedIndex > 0)
        {
            lblMessage.Text = String.Format("You selected {0} with Number : {1}", ddStudents.SelectedItem.Text,ddStudents.SelectedValue);
        }
    }

Hint: I marked the dropdownlist AppendDataBoundItems Property to true just to tell the dropdownlist to include the default listitem when the data binded..

I hope my code helps you solving your problem :)

Comments