Datagrid Problem!!!!!!!

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2007
Posts: 23
Reputation: Kshiteesh is an unknown quantity at this point 
Solved Threads: 0
Kshiteesh Kshiteesh is offline Offline
Newbie Poster

Datagrid Problem!!!!!!!

 
0
  #1
Aug 9th, 2007
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.

  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
  1. OleDb.OleDbDataAdapter(S, con)

which says
  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
  1. da.Fill(Db1DataSet1, "add1")

which shows
  1. OledbException was unhandled

showing
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 848
Reputation: QVeen72 is on a distinguished road 
Solved Threads: 120
QVeen72's Avatar
QVeen72 QVeen72 is offline Offline
Practically a Posting Shark

Re: Datagrid Problem!!!!!!!

 
0
  #2
Aug 9th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 82
Reputation: preetham.saroja is an unknown quantity at this point 
Solved Threads: 1
preetham.saroja's Avatar
preetham.saroja preetham.saroja is offline Offline
Junior Poster in Training

Re: Datagrid Problem!!!!!!!

 
0
  #3
Aug 10th, 2007
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....
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2105 | Replies: 2
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC