简体   繁体   English

从dropDownlist控件返回字符串

[英]Returning string from a dropDownlist control

I have what is a very elementary problem I realize, I am trying to return a string value from a selected value of a DropDownList upon selected index change but for some or other reason it is not happening. 我有一个非常基本的问题,我意识到,我试图在选定的索引更改时从DropDownList的选定值返回一个字符串值,但由于某些或其他原因,它没有发生。

protected void drpMinisters_SelectedIndexChanged(object sender, EventArgs e)
{
    name = drpMinisters.SelectedValue;
    LabMessage.Text = name;

}

When I try to add name to a database I get a NullReferenceException. 当我尝试将名称添加到数据库时,我得到一个NullReferenceException。

protected void butSubmitMinister_Click(object sender, EventArgs e)
{
    int index = drpMinisters.SelectedIndex;
    if (index == 0)
    {
        LabMessage.Text = "Please select a minister";
        return;
    }
    try
    {
        OleDbCommand cmd = conn.CreateCommand();
        cmd.CommandText = @"INSERT INTO MinisterTable(MinisterName)VALUES(" + name + "')";
        cmd.ExecuteNonQuery();

        LabMessage.Text = "The record was successfully added";
        conn.Close();
    }
    catch (Exception ex)
    {
        LabMessage.Text = ex.ToString();
    }


}

Advice perhaps. 也许建议。

I assume that you get the NullRefernceException on your connection object conn on this line: 我假设您在此行上的连接对象conn上获得NullRefernceException

OleDbCommand cmd = conn.CreateCommand();

Remember that all objects (also your name variable) are disposed at the end of the current page-lifecycle. 请记住,所有对象(也是您的name变量)都放在当前页面生命周期的末尾。 You can use the control's ViewState to maintain values across postbacks, fe the SelectedValue property of your DropDownList . 您可以使用控件的ViewState来维护回发之间的值,以及DropDownListSelectedValue属性。

You should also create the connection where you use it and always dispose it as soon as you're finished, best by using the using -statement. 您还应该在使用它的地方创建连接,并在完成后立即处理它,最好使用using -statement。 Otherwise other threads(requests) would need to create a new physical connection whenever this is still open: 否则,其他线程(请求)需要在仍处于打开状态时创建新的物理连接:

string sql = "INSERT INTO MinisterTable(MinisterName) VALUES(@MinisterName);";
using(var con = new OleDbConnection(connectionString))
using(var cmd = new OleDbCommand(sql, con))
{
    cmd.Parameters.AddWithValue("@MinisterName", drpMinisters.SelectedValue);
    con.Open();
    cmd.ExecuteNonQuery();
}

well i dont think your problem is with the database part, it actually lies in how you are getting the selected value from your dropdown list. 好吧,我不认为你的问题在于数据库部分,它实际上在于你如何从下拉列表中获取所选值。 if you want to get the string value that actually appears in the dropdown list, then you should use the following . 如果要获取实际显示在下拉列表中的字符串值,则应使用以下内容。

string name= (string)drpMinisters.SelectedItem;

in case you want the text in the text area of combobox then use SelectedText property 如果你想在组合框的文本区域中使用文本,那么使用SelectedText属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM