Hello again,

I have a problem with dropdownlist.

DropDownList:

<asp:DropDownList id="ddlStudents" runat="server" CssClass="textbox" OnSelectedIndexChanged="ddlStudents_SelectedIndexChanged" AutoPostBack="True" EnableViewState="False">
      <asp:ListItem Value="0">Choose</asp:ListItem>
</asp:DropDownList>

Method which fills the ddl with data from DB

protected void GetStudents()
    {
        SqlConnection conn = new SqlConnection(connection);
        SqlCommand cmd = new SqlCommand("GetFullStudentProfile", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetFullStudentProfile";
        SqlDataReader reader;
        {
            try
            {
                conn.Open();
                reader = cmd.ExecuteReader();
                reader.Read();
                while (reader.Read())
                {
                    ListItem item = new ListItem();
                    item.Text = reader["StudentName"].ToString() + " " + reader["StudentFamilyname"].ToString();
                    item.Value = reader["StudentID"].ToString();
                    ddlStudents.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                lbl.Text = ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }
    }

Method that suppose to fill lbls with retrieved data when I choose some item from this ddl.

protected void ddlStudents_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbl.Text = "";

        tbName.Visible = false;
        tbFamilyname.Visible = false;
        ddlClasses.Visible = false;
        lblBirth.Visible = true;
        tbYear.Visible = false;
        ddlDay.Visible = false;
        ddlMonth.Visible = false;
        btnSubmit.Visible = false;
        lblDay.Visible = false;
        lblMonth.Visible = false;
        lblYear.Visible = false;

        lblClass.Visible = true;
        lblFamilyName.Visible = true;
        lblName.Visible = true;
        
        SqlConnection conn = new SqlConnection(connection);
        SqlCommand cmd = new SqlCommand("ThisOne", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "ThisOne";
        cmd.Parameters.AddWithValue("@StudentId", Convert.ToInt32(ddlStudents.SelectedItem.Value));
        SqlDataReader reader;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            reader.Read();

            //Filling the controls

            lblClass.Text= reader["Classname"].ToString();
            lblBirth.Text = reader["Birthday"].ToString();
            lblName.Text = reader["StudentName"].ToString();
            lblFamilyName.Text = reader["StudentFamilyName"].ToString();
            reader.Close();
        }
        catch (Exception ex)
        {
            lbl.Text = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

    protected void PopulateClasses()
    {
        SqlConnection conn = new SqlConnection(connection);
        SqlCommand cmd = new SqlCommand("GetClassesToDDL",conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetClassesToDDL";
        SqlDataReader reader;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            reader.Read();
            while (reader.Read())
            {
                ListItem items = new ListItem();
                items.Text = reader["ClassName"].ToString();
                items.Value = reader["ClassId"].ToString();
                ddlClasses.Items.Add(items);
            }
        }
        catch (Exception ex)
        {
            lbl.Text = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

SQL Procs

create procedure [dbo].[GetFullStudentProfile]
as
select StudentID,StudentName,StudentFamilyName,Birthday,cls.Classname
from dbo.Students
inner join
Classes as cls on cls.ClassId=dbo.Students.StudentId
GO
create procedure [dbo].[ThisOne]
(@StudentId int)
as
select StudentID,StudentName,StudentFamilyName,Birthday,cls.Classname
from dbo.Students
inner join
Classes as cls on cls.ClassId=dbo.Students.StudentId
where StudentID=@studentId
GO

Problems:

1. ddl doesn't show all students from table
2. When I do insert, and it successful, ddl doesn't show new student currently inserted.
3. When I choose a student in ddl. I get "Invalid attempt to read when no data is present." error.

where's the bug and how to fix it.

P.S The page is AJAXed.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
        </ContentTemplate>
        </asp:UpdatePanel>

in masterpage.

please help thank you.

There are few thing that i should revise here.
1. when you make a instance of the SqlCommand with the Name of the stored procedure and the SqlConnection you do not have to set the CommandText property of the SqlCommand, you can delete that line.

2. I see you execute the method Read() of the DataReader with out the while loop, you dont have to do that, just do this while(Reader.Read()){code here}

3. When you finish reading thru the DataReader you have to execute the method Close of the DataReader.

first fix these things try to run and let me know what do you get.

Hello again,

I have a problem with dropdownlist.

DropDownList:

<asp:DropDownList id="ddlStudents" runat="server" CssClass="textbox" OnSelectedIndexChanged="ddlStudents_SelectedIndexChanged" AutoPostBack="True" EnableViewState="False">
      <asp:ListItem Value="0">Choose</asp:ListItem>
</asp:DropDownList>

Method which fills the ddl with data from DB

protected void GetStudents()
    {
        SqlConnection conn = new SqlConnection(connection);
        SqlCommand cmd = new SqlCommand("GetFullStudentProfile", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetFullStudentProfile";
        SqlDataReader reader;
        {
            try
            {
                conn.Open();
                reader = cmd.ExecuteReader();
                reader.Read();
                while (reader.Read())
                {
                    ListItem item = new ListItem();
                    item.Text = reader["StudentName"].ToString() + " " + reader["StudentFamilyname"].ToString();
                    item.Value = reader["StudentID"].ToString();
                    ddlStudents.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                lbl.Text = ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }
    }

Method that suppose to fill lbls with retrieved data when I choose some item from this ddl.

protected void ddlStudents_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbl.Text = "";

        tbName.Visible = false;
        tbFamilyname.Visible = false;
        ddlClasses.Visible = false;
        lblBirth.Visible = true;
        tbYear.Visible = false;
        ddlDay.Visible = false;
        ddlMonth.Visible = false;
        btnSubmit.Visible = false;
        lblDay.Visible = false;
        lblMonth.Visible = false;
        lblYear.Visible = false;

        lblClass.Visible = true;
        lblFamilyName.Visible = true;
        lblName.Visible = true;
        
        SqlConnection conn = new SqlConnection(connection);
        SqlCommand cmd = new SqlCommand("ThisOne", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "ThisOne";
        cmd.Parameters.AddWithValue("@StudentId", Convert.ToInt32(ddlStudents.SelectedItem.Value));
        SqlDataReader reader;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            reader.Read();

            //Filling the controls

            lblClass.Text= reader["Classname"].ToString();
            lblBirth.Text = reader["Birthday"].ToString();
            lblName.Text = reader["StudentName"].ToString();
            lblFamilyName.Text = reader["StudentFamilyName"].ToString();
            reader.Close();
        }
        catch (Exception ex)
        {
            lbl.Text = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

    protected void PopulateClasses()
    {
        SqlConnection conn = new SqlConnection(connection);
        SqlCommand cmd = new SqlCommand("GetClassesToDDL",conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "GetClassesToDDL";
        SqlDataReader reader;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            reader.Read();
            while (reader.Read())
            {
                ListItem items = new ListItem();
                items.Text = reader["ClassName"].ToString();
                items.Value = reader["ClassId"].ToString();
                ddlClasses.Items.Add(items);
            }
        }
        catch (Exception ex)
        {
            lbl.Text = ex.Message;
        }
        finally
        {
            conn.Close();
        }
    }

SQL Procs

create procedure [dbo].[GetFullStudentProfile]
as
select StudentID,StudentName,StudentFamilyName,Birthday,cls.Classname
from dbo.Students
inner join
Classes as cls on cls.ClassId=dbo.Students.StudentId
GO
create procedure [dbo].[ThisOne]
(@StudentId int)
as
select StudentID,StudentName,StudentFamilyName,Birthday,cls.Classname
from dbo.Students
inner join
Classes as cls on cls.ClassId=dbo.Students.StudentId
where StudentID=@studentId
GO

Problems:

1. ddl doesn't show all students from table
2. When I do insert, and it successful, ddl doesn't show new student currently inserted.
3. When I choose a student in ddl. I get "Invalid attempt to read when no data is present." error.

where's the bug and how to fix it.

P.S The page is AJAXed.

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
        </ContentTemplate>
        </asp:UpdatePanel>

in masterpage.

please help thank you.

Hello,

>Hello again,
>
>I have a problem with dropdownlist.
>
>DropDownList:

But you did not say what the error is happening to you

greeting

There are few thing that i should revise here.
1. when you make a instance of the SqlCommand with the Name of the stored procedure and the SqlConnection you do not have to set the CommandText property of the SqlCommand, you can delete that line.

2. I see you execute the method Read() of the DataReader with out the while loop, you dont have to do that, just do this while(Reader.Read()){code here}

3. When you finish reading thru the DataReader you have to execute the method Close of the DataReader.

first fix these things try to run and let me know what do you get.

Thank you Jose for pointing me to mistakes I made. I fixed them, but it didn't improve the situation with ddlStudents_SelectedIndexChanged method. It still refuses to output data into Labels.

Hello,

>Hello again,
>
>I have a problem with dropdownlist.
>
>DropDownList:

But you did not say what the error is happening to you

greeting

I said even 3 problems:

1. ddl doesn't show all students from table
2. When I do insert, and it successful, ddl doesn't show new student currently inserted.
3. When I choose a student in ddl. I get "Invalid attempt to read when no data is present." error.

Ok. Now ddlStudents_SelectedIndexChanged works. The problem was EnableViewState="true" assigned to ddl control. Don't remember why I did add this property to control.

Thanks to Jose who did help me much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.