Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
链接文本框C#SQL Server_C#_Sql Server - Fatal编程技术网

链接文本框C#SQL Server

链接文本框C#SQL Server,c#,sql-server,C#,Sql Server,如何链接文本框 情景: TextBox_Supplier TextBox_Address TextBox\u供应商已自动完成,并且正在运行。在供应商的文本框中键入时,文本框地址将选择供应商的地址 我的代码不起作用: private void txb_vendor_TextChanged(object sender, EventArgs e) { if (string.IsNullOrEmpty(txb_address.Text)) { PurCon.con.Op

如何链接文本框

情景:

TextBox_Supplier
TextBox_Address
TextBox\u供应商
已自动完成,并且正在运行。在供应商的
文本框中键入时,
文本框地址将选择供应商的地址

我的代码不起作用:

private void txb_vendor_TextChanged(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(txb_address.Text))
    {
        PurCon.con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = PurCon.getcon();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = String.Format("SELECT address FROM tbl_Supplier WHERE supplier_name = {0}",txb_vendor.Text);

        SqlDataReader red = cmd.ExecuteReader();

        while (red.Read())
        {
            string address = red.GetString(0);
            address = txb_address.Text;
        }

        PurCon.con.Close();
    }
}
谢谢你帮助我

而不是

address = txb_address.Text;

txb_address.Text = address;

尝试使用参数化查询,而不是串接

按照@Mohit的建议进行更改,并用单引号将供应商名称括起来,因为供应商名称是字符串类型,在sql中,字符串应该用单引号括起来,否则会出现sql错误

"SELECT address FROM tbl_Supplier WHERE supplier_name = '{0}'"
                                                    ----^   

我上周已经解决了这个问题。我对自己很生气

textbox\u地址不会更改一次,当
textbox\u供应商\u text更改时,它会堆叠起来。所以我使用Clear()方法来清除前面的输入地址

public void AddressTbxLoad()
    {

        DBCon PurCon = new DBCon();
        PurCon.con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = PurCon2.con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = String.Format("SELECT address FROM tbl_Supplier WHERE supplier_name LIKE '{0}%'", cbx_vendor.Text);
        SqlDataReader red = cmd.ExecuteReader();

        while (red.Read())
        {
            string address = red.GetString(0);
            txb_address.Text = address;

        }
        PurCon.con.Close();
    }

    private void cbx_vendor_SelectedIndexChanged(object sender, EventArgs e)
    {
        txb_address.Clear();
        AddressTbxLoad();
    }

    private void Purchase_Load(object sender, EventArgs e)
    {
        VendorTbxLoad();
    }

先生@mohit shrivastava代码有效。但是文本框地址只改变了一次。就像我键入一个字符一样,它会给我一个与我键入的字符相关的随机地址。我想有一个同步的文本框,当输入地址时,始终更改,直到文本框完成。我在VB.Net中这样做,但在C中做不到。请尝试在
LostFocus
事件中,而不是在
TextChanged
中这样做。只有在您离开文本框后,才会填充地址。这也可以节省你每次按键时的数据库调用。先生@mohit shrivastava我没有看到
LostFocus
事件,而是看到了
Leave
事件。你是这么说的吗?我尝试“离开”事件。起初它是有效的,但当我试图更改供应商名称时,地址没有改变。就像只有一次一样。@AnjelloJoshua:这是一件同样的事情,在你的情况下,
Leave
LostFocus
,当你深入内心的时候,它有点不同。但在您的情况下,您可以实现
Leave
事件以获得所需的结果。希望有帮助,我会得到答案。:)