Public Sub auto_Suggest(ByVal member As String, ByVal table As String, ByVal txt As Object)
Try
dta = New DataTable
'open the connection
conn.Open()
'holds the data in the database
With cmda
.Connection = conn
.CommandText = "select " & member & " from " & table
End With
'''''''''''''''fill data in the table
daa.SelectCommand = cmda
daa.Fill(dta)
''function of autocomplete
Dim r As DataRow
txt.AutoCompleteCustomSource.Clear()
For Each r In dta.Rows
txt.AutoCompleteCustomSource.Add(r.Item(0).ToString)
Next
''''''''''''''''''''''''
Catch ex As Exception
MsgBox(ex.Message)
End Try
''''close the connection
conn.Close()
daa.Dispose()
End Sub
Private Sub Stock_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
'call a public sub
'the job_id is the field of a table , the employees is the name of a table and a textbox is an object
auto_Suggest("ItemName", "stock", TxtItemName)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I got this error for auto _Suggest and error is "Object reference not set an instance of an object "
Asked
Active
Viewed 41 times
0

Chris Dunaway
- 10,974
- 4
- 36
- 48

janagan
- 15
- 1
- 7
-
1possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Nathan Tuggy Mar 29 '15 at 04:27
1 Answers
0
First, it's good to employ a "use it and lose it" approach and take advantage of Using blocks to automatically dispose of connections, commands, and readers. See the example below.
Second, please set a breakpoint on the first line after the Try statement and step through your source in the debugger. If the exception is reached, inspect the ex variable and potentially the ex.InnerException (if any).
Last, the example here uses SQLConnection and SQLCommand (SQL Server). Just swap out whatever library you are using for MySQL and you should be good to go.
Public Sub auto_Suggest(connectionString As String, member As String, table As String, txt As TextBox)
Try
txt.AutoCompleteCustomSource.Clear()
Using cn = New SqlConnection(connectionString)
cn.Open()
Using cmd = New SqlCommand("SELECT " & member & " FROM " & table, cn)
Using dr = cmd.ExecuteReader()
While dr.Read
txt.AutoCompleteCustomSource.Add(dr(member).ToString)
End While
End Using
End Using
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Craig Johnson
- 744
- 4
- 8
-
Error 1 Type 'SqlConnection' is not defined. D:\Pharmacy\Pharmacy\Surgical Stock.vb 125 28 Pharmacy and Error 2 Type 'SqlCommand' is not defined. D:\Pharmacy\Pharmacy\Surgical Stock.vb 128 33 Pharmacy and I got this errrors i don't know what is this eror – janagan Mar 29 '15 at 16:16
-
Thank you soo much i got it ya thanks a lot Can you help me for this work Iwood like to reduct stock quantity when i seel the product .If you like please help me ihave done like this – janagan Mar 29 '15 at 16:26
-
cmd = New MySqlCommand("INSERT INTO sale VALUES (null,'" & txt_billno.Text.ToString() & "','" & txtdate.Text.ToString() & "','" & TxtCName.Text.ToString() & "','" & txtItCode.Text.ToString() & "','" & TxtItName.Text.ToString() & "','" & Val(txtQty.Text) & "','" & txtPrice.Text.ToString() & "','" & Val(txtFreIsue.Text) & "','" & txtAmount.Text.ToString & "')", conn) CtQty = TxtQTYSTOCK.Text -sellQity cmd = New MySqlCommand("UPDATE `pharmacy`.`stock` SET `Quantity`='" & CurrentQuantity & "' WHERE `stock`.`ItemCode` = '" & txtItemCode.Text.ToString() & "'", conn) – janagan Mar 29 '15 at 16:35
-
Here's how to debug SQL code - set a breakpoint prior to each SQL command executing. At the breakpoint, copy the command text exactly as you have it in each MySqlCommand("{command text}"). Then print the text and you should see the resulting SQL statement. Inspect and/or run this statement directly for the db. You will see any issues quickly, I guarantee. Also, just as an aside, where you have statements like txt_billno.Text.ToString(), .Text properties are already strings so no need to have .ToString() there :). If you like, post the full code as a new question and I will take a look at it. – Craig Johnson Mar 29 '15 at 17:16