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.