开发者

how to declare sql parametrs in vb.net

开发者 https://www.devze.com 2023-02-25 03:47 出处:网络
hi i need to get the customer name from the user as in combo box when the user selets the user i want that selected customer name should be searched in the sql table (here table name is \"obbalance \"

hi i need to get the customer name from the user as in combo box when the user selets the user i want that selected customer name should be searched in the sql table (here table name is "obbalance ")and all the entries in the table having the name as the selected customer naem it should be shown in the data grid view

cmd.Parameters.Add(New SqlParameter("@p1", SqlDbType.NVarChar).Value = ComboBox1.SelectedItem.ToString)

cmd = New SqlCommand("select obbalance from balance where custname=@p1", con)

dr = cmd.ExecuteReader()
Form2.Show()

after thios also it shows a error plz can u help me out if the code is wrong den help me to do correc开发者_Python百科t it i am new to vb.net plz .......... it shows error in declaration so can u send me any other code or if this code den plz send the correct code plz


You need to add the parameter after creating the new command:

cmd = New SqlCommand("select obbalance from balance where custname=@p1", con)

cmd.Parameters.Add(New SqlParameter("@p1", SqlDbType.NVarChar).Value = ComboBox1.SelectedItem.ToString)

dr = cmd.ExecuteReader()
Form2.Show()

Additionally, you are creating a boolean value as the parameter to the Add method:

New SqlParameter("@p1", SqlDbType.NVarChar).Value = ComboBox1.SelectedItem.ToString

Do this instead:

Dim param as SqlParameter = New SqlParameter("@p1", SqlDbType.NVarChar)
param.Value = ComboBox1.SelectedItem.ToString)
cmd.Parameters.Add(param)


 cmd.Parameters.Add(New SqlParameter("@p1", SqlDbType.NVarChar).Value = ComboBox1.SelectedItem.ToString)

cmd = New SqlCommand("select obbalance from balance where custname=@p1", con)

dr = cmd.ExecuteReader()

Instead of doing this you can do this also

cmd=New SqlCommand("select columnname from tablename where custname='"& ComboBox1.SelectedItem.ToString &"'",con)
 dr = cmd.ExecuteReader()

Why do you want parameterized query

0

精彩评论

暂无评论...
验证码 换一张
取 消