I have inserted searching through passing parameter likewise. In search form there is only one textbox to pass the value. But there are three field names which are associated with that text box.
For example: I have searched by passing phone number
But there three field name tel1, tel2 and tel3 which which are called when the phone number is passed. my code is as follows. my frontend is vb.net and my backend is ms access.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Db1DataSet1.Clear() Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=K:\Database\db1.mdb" Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection(conStr) Dim S As String Dim datagrid1 As New DataGrid Dim tmp As New ADODB.Recordset tmp.Open("add1", conStr, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic) If TextBox1.Text = tmp.Fields("tel1").Value Then S = "select * from add1 where tel1='" & TextBox1.Text & "'" ElseIf TextBox1.Text = tmp.Fields("tel2").Value Then S = "select * from add1 where tel2='" & TextBox1.Text & "'" ElseIf TextBox1.Text = tmp.Fields("tel3").Value Then S = "Select * from add1 where tel3='" & TextBox1.Text & "'" ElseIf Not (tmp.EOF = True And tmp.BOF = True) Then : MsgBox("no record found") End If Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(S, con) da.Fill(Db1DataSet1, "add1") datagrid1.DataSource = Db1DataSet1.DefaultViewManager End Sub
In this snippet I have faced one problem. It shows one build error at
Variable 'S' is used before it has been assigned a value. A null reference exception could result at runtime.
And when I run this program it executes until I pass the values present in the field , but when i pass the values not present in the fields then the msg box is shown as mentioned the program...but the problem persists when I click the ok button of msg box or try to stop debugging.
It shows the error at
OledbException was unhandled
Command text was not set for the command object.
I m a beginner of VB.net. so please help me what type of error is this and how can I solve this problem. please provide me with appropriate code to resolve the problem.
Anticipating the help.