944,017 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 2752
  • VB.NET RSS
Aug 9th, 2007
0

Datagrid Problem!!!!!!!

Expand Post »
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.

vb.net Syntax (Toggle Plain Text)
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. [inlinecode]
  3.  
  4. Db1DataSet1.Clear()
  5. Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=K:\Database\db1.mdb"
  6. Dim con As OleDb.OleDbConnection = New OleDb.OleDbConnection(conStr)
  7. Dim S As String
  8. Dim datagrid1 As New DataGrid
  9.  
  10. Dim tmp As New ADODB.Recordset
  11. tmp.Open("add1", conStr, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
  12.  
  13. If TextBox1.Text = tmp.Fields("tel1").Value Then
  14. S = "select * from add1 where tel1='" & TextBox1.Text & "'"
  15. ElseIf TextBox1.Text = tmp.Fields("tel2").Value Then
  16. S = "select * from add1 where tel2='" & TextBox1.Text & "'"
  17. ElseIf TextBox1.Text = tmp.Fields("tel3").Value Then
  18. S = "Select * from add1 where tel3='" & TextBox1.Text & "'"
  19.  
  20. ElseIf Not (tmp.EOF = True And tmp.BOF = True) Then : MsgBox("no record found")
  21. End If
  22. Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(S, con)
  23. da.Fill(Db1DataSet1, "add1")
  24. datagrid1.DataSource = Db1DataSet1.DefaultViewManager
  25. [/inlinecode]
  26. End Sub

In this snippet I have faced one problem. It shows one build error at
VB.NET Syntax (Toggle Plain Text)
  1. OleDb.OleDbDataAdapter(S, con)

which says
VB.NET Syntax (Toggle Plain Text)
  1. 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
VB.NET Syntax (Toggle Plain Text)
  1. da.Fill(Db1DataSet1, "add1")

which shows
VB.NET Syntax (Toggle Plain Text)
  1. OledbException was unhandled

showing
VB.NET Syntax (Toggle Plain Text)
  1. Command text was not set for the command object.
error.

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.

Regards

Kshiteesh
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kshiteesh is offline Offline
23 posts
since Jun 2007
Aug 9th, 2007
0

Re: Datagrid Problem!!!!!!!

Hi Kshiteesh,

few points what i have noted here...:

1. Before Creating the DataAdapter, u are not checking if "S" is blank or not.. the error which u have may be the same..
or else in ur If condition if nothing found, then exit sub..

2. Use a new Dataset. Check this:

Dim ds AsNew DataSet
da.Fill(ds, "add1")
DataGrid1.DataSource = ds.DefaultViewManager


REgards
Veena
Last edited by QVeen72; Aug 9th, 2007 at 7:25 am.
Reputation Points: 84
Solved Threads: 140
Posting Shark
QVeen72 is offline Offline
923 posts
since Nov 2006
Aug 10th, 2007
0

Re: Datagrid Problem!!!!!!!

always make sure,u declare the
str,connectionstring,sqlcommand,sqldataadapter in this order..
Even if u try to access adapter b4 sqlcommand-then u get the errors(which u have got)...so,make sure first they are declared& then there objects are accesed....
Reputation Points: 5
Solved Threads: 1
Junior Poster in Training
preetham.saroja is offline Offline
82 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Using RecordsAffected as a parameter in an IF-THEN-ELSE statement
Next Thread in VB.NET Forum Timeline: to include dropdownlist,in datagrid....





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC