试图从 sql 数据库中获取数据:输入文本框的内容 [英] Trying to get a data from sql database: what to enter into textbox

查看:41
本文介绍了试图从 sql 数据库中获取数据:输入文本框的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 txtitemcode 的文本框,当我在文本框中输入项目代码时,我想将相应的数据显示到网格视图或文本框中.

I have text-box named txtitemcode, when I type the item-code into text box I want to to display the corresponding data into grid-view or text boxes.

但它并没有触发代码,我不知道我错在哪里,我在谷歌搜索并不断更改代码,最后我仍然无法正常工作.我在这里粘贴我的代码..

But it's not firing the code, I don't know where I am wrong, I search in the Google and keep change the codes , finally I got one still its not working. I am pasting my code here..

Public Sub SelectItem(ByVal ItemCode As String)
        Try
            sql.OpenDbConnection()
            Dim strSQL As String = "SELECT ItemCode as 'Item Code',ItemName as 'Item Name' FROM tblItemMaster where ItemCode='ItemCode'"
            'SqlConn.Close()
            Dim da As New SqlDataAdapter(strSQL, sql.SqlConn)
            Dim ds As New DataSet
            da.Fill(ds, "tblItemMaster")
            dgvPurchaseOrder.DataSource = ds.Tables("tblItemMaster")
            sql.SqlConn.Close()
        Catch ex As SqlException
            MsgBox(ex.Message, MsgBoxStyle.Critical, "SQL Error")
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")
        End Try
    End Sub

我从文本lostfocus、click和一些可疑事件中调用这个类,没有运气..

I call this class from text lostfocus, click and some of doubted events, no luck ..

推荐答案

您没有正确传递参数.试试这个:

You are not passing the parameter correctly. Try this:

Dim strSQL As String = "SELECT ItemCode 'Item Code',ItemName 'Item Name' " & _
    " FROM tblItemMaster where ItemCode= @ItemCode"
Dim cmd As New SqlCommand(strSQL, sql.SqlConn)
Dim ds As New DataSet

cmd.Parameters.AddWithValue("ItemCode", ItemCode)

Dim da As New SqlDataAdapter(cmd)
da.Fill(ds, "tblItemMaster")

查看 MSDN 文档 关于它.

这篇关于试图从 sql 数据库中获取数据:输入文本框的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆