Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Second request to DB doesn't work

Status
Not open for further replies.

passs

Programmer
Dec 29, 2003
170
RU
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(&quot;SELECT Year FROM Year WHERE YearID =&quot; + 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(&quot;SELECT MAX(MonthID) FROM Month&quot;, 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(&quot;SELECT Month FROM Month WHERE MonthID =&quot; + iCount + &quot; AND &quot; + year + &quot;=1&quot;, 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
 
I don't understand this bit:
[tt] &quot; AND &quot; + year + &quot;=1&quot;[/tt]

Isn't year the year the user selected?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top