 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 4th, 2005, 03:26 PM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

August 4th, 2005, 03:40 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
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.
|
|

August 4th, 2005, 04:05 PM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

August 4th, 2005, 04:36 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
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.
|
|

August 5th, 2005, 02:33 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

August 5th, 2005, 08:40 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
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.
|
|

August 5th, 2005, 08:45 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hiya,
Thanks for the code about the nulls, will give it a try.
Yes,I did get my dataset from the toolbox.
B
|
|

August 5th, 2005, 08:48 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
So now I guess I need to check if my dataset is configured properly...Any ideas?
B
|
|

February 6th, 2006, 01:39 PM
|
|
Registered User
|
|
Join Date: Feb 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |