为什么我的下拉列表没有填满文本框?

时间:2013-12-10 07:26:18

标签: c# database drop-down-menu

我有一个名为IsAuthor的下拉列表项。它不会通过从tblnewgroup表中检索值来填充我的文本框。我无法理解的问题在哪里,并没有显示任何错误,请帮助我

protected void lstAuthor_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectSQL;
    selectSQL = "SELECT * FROM tblnewgroup ";
    selectSQL += "WHERE Groupno='" + lstAuthor.SelectedItem.Value + "'";
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    SqlDataReader reader;

try
{
    con.Open();
    reader = cmd.ExecuteReader();
    reader.Read();

    txtGn.Text = reader["Groupno"].ToString();
    txtgname.Text=reader["Groupname"].ToString();
    txtsl.Text=reader["Slno"].ToString();
    txtsn.Text = reader["Subname"].ToString();

    reader.Close();
    lblResults.Text = "";
}
catch (Exception err)
{
    lblResults.Text = "Error getting author. ";
    lblResults.Text += err.Message;
}
finally
{
    con.Close();
}

}

我用这些代码填写了我的下拉列表..

private void FillAuthorList()
        {
            lstAuthor.Items.Clear();
            string selectSQL = "SELECT Groupname, Groupno FROM tblnewgroup";

            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(selectSQL, con);
            SqlDataReader reader;

            try
            {
                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ListItem newItem = new ListItem();
                    newItem.Text = reader["Groupname"].ToString();
                    newItem.Value = reader["Groupno"].ToString();
                    lstAuthor.Items.Add(newItem);
                }
                reader.Close();
            }
            catch (Exception err)
            {
                lblResults.Text = "Error reading list of names. ";
                lblResults.Text += err.Message;
            }
            finally
            {
                con.Close();
            }
        }

4 个答案:

答案 0 :(得分:1)

我假设SqlDataReader.Read方法返回boolean值,您可以像使用它一样使用

while(reader.Read())
{
    txtGn.Text = reader["Groupno"].ToString();
    txtgname.Text=reader["Groupname"].ToString();
    txtsl.Text=reader["Slno"].ToString();
    txtsn.Text = reader["Subname"].ToString();
}

此外,我怀疑lstAuthor.SelectedItem.Valuenumber而不是string。您可能需要在不使用""的情况下使用它。

同样使用parameterized queries总是一个不错的选择。

selectSQL = "SELECT * FROM tblnewgroup WHERE Groupno = @Groupno";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
cmd.Parameters.AddWithValue("@Groupno", lstAuthor.SelectedItem.Value);
....

答案 1 :(得分:1)

问题:您要为单个号码Groupno分配单引号。
解决方案:您需要将单引号分配给VARCHAR类型。

建议:您只是在不检查行的情况下从SqlDataReader对象中分配值。如果找不到行,则会抛出Exeption。因此,在将值分配给TextBox控件之前,我建议将Ceck用于任何行的SqlDataReader对象。

试试这个:

protected void lstAuthor_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectSQL;
    selectSQL = "SELECT * FROM tblnewgroup ";
    selectSQL += "WHERE Groupno=" + lstAuthor.SelectedValue;
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    SqlDataReader reader;

try
{
    con.Open();
    reader = cmd.ExecuteReader();
    if(reader.Read())
    {
    txtGn.Text = reader["Groupno"].ToString();
    txtgname.Text=reader["Groupname"].ToString();
    txtsl.Text=reader["Slno"].ToString();
    txtsn.Text = reader["Subname"].ToString();
    lblResults.Text = "Data Updated Successfully!";
    }
    else
    {
      lblResults.Text = "No Records found!";
    }
    reader.Close();

}
catch (Exception err)
{
    lblResults.Text = "Error getting author. ";
    lblResults.Text += err.Message;
}
finally
{
    con.Close();
}

}

答案 2 :(得分:1)

看起来您正在选择数字字段,但将其视为文本字段。修改您的查询以删除单引号:

selectSQL = "SELECT * FROM tblnewgroup ";
selectSQL += "WHERE Groupno=" + lstAuthor.SelectedItem.Value;

同时检查reader.read()的返回值以查看是否有任何记录:

using(var reader = cmd.ExecuteReader())
{
    if(reader.Read())
    {
        txtGn.Text = reader["Groupno"].ToString();
        txtgname.Text=reader["Groupname"].ToString();
        txtsl.Text=reader["Slno"].ToString();
        txtsn.Text = reader["Subname"].ToString();

    }
    else
    {
        lblResults.Text = "Author not found";
    }
}

请注意,我已将阅读器放在using块中以确保它已关闭,即使发生异常也是如此。

答案 3 :(得分:0)

使用以下内容:

while (reader.Read())
{
    txtGn.Text = reader["Groupno"].ToString();
txtgname.Text=reader["Groupname"].ToString();
txtsl.Text=reader["Slno"].ToString();
txtsn.Text = reader["Subname"].ToString();

}