簡體   English   中英

將數據從SQL Server獲取到文本框錯誤消息

[英]Get data from sql server into textbox error message

protected void DropDownServerName_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);

        conn.Open();

        string serverName = DropDownServerName.SelectedValue;

        string sqlquery = ("SELECT Architecture FROM tblServer WHERE (ServerName = " + serverName + ")");

        SqlCommand command = new SqlCommand(sqlquery, conn);

        txtUpdateArchitecture.Text = command.ExecuteScalar().ToString();

        conn.Close();
    }

DropDownServerName區域使用SqlDataSource連接到SQL Server,以在ServerName列上顯示值列表。

獲得名為“ Brad”的選擇值后,我希望Brad的“體系結構”列中的值顯示在文本框中。 但是我得到了錯誤提示,無效的列名“ Brad”。 該列假定為ServerName,而Brad只是ServerName列中的一個值。

您需要在服務器名前后加引號

string sqlquery = ("SELECT Architecture FROM tblServer WHERE (ServerName = '" + serverName + "')");

或更妙的是,使用參數化查詢[這更安全,可防止SQL注入和字符串中的有趣字符污染您的查詢]

string sqlquery = "SELECT Architecture FROM tblServer WHERE ServerName = @ServerName";

SqlCommand command = new SqlCommand(sqlquery, conn);
command.Parameters.AddWithValue("@ServerName", serverName);

在WHERE子句的servername周圍添加'':

... WHERE ServerName = '" + serverName + "' ...

試試這個吧。 它應該使用單引號。

string sqlquery = ("SELECT Architecture FROM tblServer WHERE ServerName = '" + serverName + "'");

您可能缺少變量周圍的單引號。 嘗試這個

    protected void DropDownServerName_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Shared_NotebookConnectionString"].ConnectionString);

        conn.Open();

        string serverName = DropDownServerName.SelectedValue;

        string sqlquery = ("SELECT Architecture FROM tblServer WHERE (ServerName = '" + serverName + "')");

        SqlCommand command = new SqlCommand(sqlquery, conn);

        txtUpdateArchitecture.Text = command.ExecuteScalar().ToString();

        conn.Close();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM