Hello Everybody!
Maybe someone can help me and show where am i wrong?
Look, i have first dropdownlist, which is fullfilling with data from DB in Page_Load. After user chose something in this dropdownlist request sends to server (autopostback) and i have to show another dropdownlist with data from another table of DB and concerns to data from first dropdownlist.
I use folowing code:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
SqlCommand MaxID = new SqlCommand("SELECT MAX(YearID) FROM Year", sqlConnection);
//open connection
sqlConnection.Open();
int num_of_str, jCount=0;
num_of_str = (int)MaxID.ExecuteScalar();
for (int iCount = 1; iCount <= num_of_str; iCount++)
{
//create command object
SqlCommand cmdGetCalls = new SqlCommand("SELECT Year FROM Year WHERE YearID =" + iCount, sqlConnection);
//create reading data object
SqlDataReader dreadCalls;
//do a command
dreadCalls = cmdGetCalls.ExecuteReader();
//show strings
while (dreadCalls.Read())
{
ddlYear.Items.Add(new ListItem(dreadCalls.GetInt32(0).ToString(),dreadCalls.GetInt32(0).ToString()));
}
jCount++;
//close reading data object
dreadCalls.Close();
}
sqlConnection.Close();
}
else
{
SqlCommand MaxIDMonth = new SqlCommand("SELECT MAX(MonthID) FROM Month", sqlConnection);
//open connection
sqlConnection.Open();
int num_of_str_m, jCount=0;
string year;
year = ddlYear.SelectedItem.Value.ToString();
Response.Write(year);
num_of_str_m = (int)MaxIDMonth.ExecuteScalar();
for (int iCount = 1; iCount <= num_of_str_m; iCount++)
{
//create command object
SqlCommand cmdGetMonth = new SqlCommand("SELECT Month FROM Month WHERE MonthID =" + iCount + " AND " + year + "=1", sqlConnection);
//create reading data object
SqlDataReader readMonth;
//do a command
readMonth = cmdGetMonth.ExecuteReader();
//show strings
while (readMonth.Read())
{
Response.Write(readMonth.GetString(0));
ddlMonth.Items.Add(new ListItem(readMonth.GetString(0),readMonth.GetString(0)));
}
jCount++;
//close reading data object
readMonth.Close();
}
sqlConnection.Close();
ddlMonth.Visible = true;
}
//sqlConnection.Close();
}
first dropdown menu works well, second is appiaring after postback, but it's list is empty. I don't know why.
Thank you.
Best regards,
Alexander
Maybe someone can help me and show where am i wrong?
Look, i have first dropdownlist, which is fullfilling with data from DB in Page_Load. After user chose something in this dropdownlist request sends to server (autopostback) and i have to show another dropdownlist with data from another table of DB and concerns to data from first dropdownlist.
I use folowing code:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
SqlCommand MaxID = new SqlCommand("SELECT MAX(YearID) FROM Year", sqlConnection);
//open connection
sqlConnection.Open();
int num_of_str, jCount=0;
num_of_str = (int)MaxID.ExecuteScalar();
for (int iCount = 1; iCount <= num_of_str; iCount++)
{
//create command object
SqlCommand cmdGetCalls = new SqlCommand("SELECT Year FROM Year WHERE YearID =" + iCount, sqlConnection);
//create reading data object
SqlDataReader dreadCalls;
//do a command
dreadCalls = cmdGetCalls.ExecuteReader();
//show strings
while (dreadCalls.Read())
{
ddlYear.Items.Add(new ListItem(dreadCalls.GetInt32(0).ToString(),dreadCalls.GetInt32(0).ToString()));
}
jCount++;
//close reading data object
dreadCalls.Close();
}
sqlConnection.Close();
}
else
{
SqlCommand MaxIDMonth = new SqlCommand("SELECT MAX(MonthID) FROM Month", sqlConnection);
//open connection
sqlConnection.Open();
int num_of_str_m, jCount=0;
string year;
year = ddlYear.SelectedItem.Value.ToString();
Response.Write(year);
num_of_str_m = (int)MaxIDMonth.ExecuteScalar();
for (int iCount = 1; iCount <= num_of_str_m; iCount++)
{
//create command object
SqlCommand cmdGetMonth = new SqlCommand("SELECT Month FROM Month WHERE MonthID =" + iCount + " AND " + year + "=1", sqlConnection);
//create reading data object
SqlDataReader readMonth;
//do a command
readMonth = cmdGetMonth.ExecuteReader();
//show strings
while (readMonth.Read())
{
Response.Write(readMonth.GetString(0));
ddlMonth.Items.Add(new ListItem(readMonth.GetString(0),readMonth.GetString(0)));
}
jCount++;
//close reading data object
readMonth.Close();
}
sqlConnection.Close();
ddlMonth.Visible = true;
}
//sqlConnection.Close();
}
first dropdown menu works well, second is appiaring after postback, but it's list is empty. I don't know why.
Thank you.
Best regards,
Alexander