Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old August 4th, 2005, 03:26 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Brettvan1
Default

I hate to be a pain but my code is just not working...Can't understand it...

This is what I have, can you tell me where I am going wrong?
    Dim Ds As DataSet1

    Public Sub BindComboBox()
        ComboBox1.DataSource = Ds.Tables("Customers")
        ComboBox1.DisplayMember = "CustomerID"

    End Sub

 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        TextBox1.Text = Ds.Tables("Customers").Rows(ComboBox1.SelectedInde x).Item(1)
        TextBox2.Text = Ds.Tables("Customers").Rows(ComboBox1.SelectedInde x).Item(2)
        TextBox3.Text = Ds.Tables("Customers").Rows(ComboBox1.SelectedInde x).Item(3)
        TextBox4.Text = Ds.Tables("Customers").Rows(ComboBox1.SelectedInde x).Item(4)
        TextBox5.Text = Ds.Tables("Customers").Rows(ComboBox1.SelectedInde x).Item(5)

    End Sub


End Class
Thanks agin.
B

Reply With Quote
  #12 (permalink)  
Old August 4th, 2005, 03:40 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

A couple of things.

Change: Dim Ds As DataSet1
To: Dim Ds As DataSet = New DataSet

(what is DataSet1???)

Also note that you will have to check for DBNulls before you try to set the textbox values to the values in the dataset or it will throw an error.

If this doesn't help, post the error.
Reply With Quote
  #13 (permalink)  
Old August 4th, 2005, 04:05 PM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Brettvan1
Default

Ok, changed the DataSet but keep getting this error

An unhandled exception of type 'System.NullReferenceException' occurred in Stage4 Assignment 2 Doughnut Shop Orders.exe

Additional information: Object reference not set to an instance of an object.

B

Reply With Quote
  #14 (permalink)  
Old August 4th, 2005, 04:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

You aren't instantiating something. Does it give you a line number and what object does it point to on that line?

Or...another possibility (this is what I snagged on at first)

You have a null field in one of your dataset rows/columns that is being pulled from the database. This is where you will have to check your dataset for null values.
Reply With Quote
  #15 (permalink)  
Old August 5th, 2005, 02:33 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Brettvan1
Default

The error is generated on this line...
TextBox1.Text = Ds.Tables("Customers").Rows(ComboBox1.SelectedInde x).Item(1)

How do I check for Null values?

I do apologise for all the questions, but I am still fairly new to all of this.

Thanks again
B


Reply With Quote
  #16 (permalink)  
Old August 5th, 2005, 08:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Don't worry about it. Everyone is new to it at one time or another.

---------------------------

If Not Convert.IsDBNull(Ds.Tables("Customers").Rows(Combo Box1.SelectedIndex).Item(1)) Then
TextBox1.Text = Ds.Tables("Customers").Rows(ComboBox1.SelectedInde x).Item(1)
End If

---------------------------

I have another question. Did you originally add your dataset in code or did you grab it from the toolbox? I ask this because, looking back at your post where you have:

Dim Ds As DataSet1

This looks like a name that is given when the dataset is dropped from the toolbox. I do mine in code so I set the properties manually. This could be causing some of the issues if your dataset isn't configured properly.
Reply With Quote
  #17 (permalink)  
Old August 5th, 2005, 08:45 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Brettvan1
Default

Hiya,

Thanks for the code about the nulls, will give it a try.

Yes,I did get my dataset from the toolbox.

B

Reply With Quote
  #18 (permalink)  
Old August 5th, 2005, 08:48 AM
Authorized User
 
Join Date: Sep 2004
Location: , , .
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Brettvan1
Default

So now I guess I need to check if my dataset is configured properly...Any ideas?
B

Reply With Quote
  #19 (permalink)  
Old February 6th, 2006, 01:39 PM
Registered User
 
Join Date: Feb 2006
Location: , , United Kingdom.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know this is an old post, but others might find this useful. This example was done in .NET 2003.

Add your combo box and text boxes etc. to the form. Set the DropDownStyle(In appearance) of the combo box to DropDownList(This prevents users from adding to the combo box).

Click the view menu item and click server viewer. Right click on data connections(in server viewer) and add a connection to your database.

Click view toolbox and make sure your form is selected. In the data tab (must be in form design view and not code view) of the toolbox double click OleDBDataAdapter and follow the wizard.

Right click on the oledbdataadapter1 you just created and left click on Generate Dataset. Use the query builder if you like to build the SQL string that will be used to select the relevant record(s) from the relevant table(s) in your database.

Doubleclick your form and this should open the code window at the FormLoad event. Add this code:

Try
  'Clear Datasourse, Dataset11 is the name of the Dataset you created, this might need changed to the name of yours!
  DataSet11.Clear()
  'Fill Datasource
'The dataadapter name may need changed to the name of the one you created, plus the dataset may need changed!
  OleDbDataAdapter1.Fill(DataSet11)
Catch ex As Exception
   ex.ToString()
End Try

Edit the properties of your combo box to these:

Datasource - (Select your Dataset, there should be two items to select from, select the one with the shortest name!)
Display Member - To solve this specific post, select the ID of the table to display.

Set the properties of your text boxes to Enabled = False or Locked = true. This means the data will be read only.

Set the text value in the databindings properties of each text box to the relevant field in the dataset.

That's your read only Database viewer complete.

Dave
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get data from Excel file using ADO.NET lam_lvl ADO.NET 7 February 27th, 2014 05:19 AM
confusing in ADO.NET Entity Data Model,Linq to sql angelboy ADO.NET 5 November 21st, 2008 05:01 PM
Data management with ADO.NET GriffithsJ BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 1 February 13th, 2006 12:57 PM
VB6 ADO Dynamic Data Binding Question pad Pro VB Databases 1 January 28th, 2005 10:43 PM



All times are GMT -4. The time now is 07:49 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.