VB.NET

Moderators: seancampbell
Number of threads: 3883
Number of posts: 9849

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
need help!! Posted by azdonald on 17 Nov 2009 at 9:06 AM
On my form load event, i populate three comboboxes by code.... in the button click event i try to insert into the database and i use combobox.selecteditem and it generates an insert error.
but when i use combobox.selectedindex, it runs.
dont know wat to do.
Report
Re: need help!! Posted by seancampbell on 17 Nov 2009 at 10:31 AM
Have you looked at the contents of the Insert Statement to see why it is giving you an insert error? It'd be very helpful, for me to give you a good answer, if you provided the exact error messages and some sample code...

Here is a suggestion on something to try so you can understand why your statement is crashing:

MsgBox(ComboBox1.SelectedItem.GetType.ToString, vbOkOnly, "Test")

You'll see that you get a type like "ListItem" or something from ComboBox1.SelectedItem. That is because ComboBox1.SelectedItem returns an OBJECT, not a VALUE, and that OBJECT is determined by the Datasource of the ComboBox (in this case you have added an item manually with ComboBox.Items.Add, so it uses a default data type and adds it to the datasource).....

So here is some things you can use:

ComboBox1.SelectedText 'this should work
ComboBox1.SelectedValue 'this works when you have a datasource and a valuemember set
ComboBox1.Items(ComboBox1.SelectedItem).Text

Here is an example of how to check if something is selected, and get the value:
'Assume ComboBox1 is the name of your combobox control
Dim ComboVal as STring = ""
If ComboBox1.SelectedValue > -1 Then 'Something is selected
  ComboVal = ComboBox1.SelectedText
Else
  'By Default you can type text into a ComboBox, if there is
  'text in the combo, and you haven't selected any, you can do
  'something here
End If





Report
Re: need help!! Posted by azdonald on 18 Nov 2009 at 1:14 AM
hey seancampbell this is my code...
ON form load event..
Dim conn As New MySqlConnection
conn.ConnectionString = "server=localhost;" _
& "database=cargomanager;" _
& "userid=root;" _
& "password=admin;"
Try
conn.Open()
Catch myerror As MySqlException
MsgBox("failed to connect to database")
End Try
sql0 = "select name as cname from agent"
sql1 = "select name as dname from airline"
sql2 = "select abbreviation as ename from destination"
command.Connection = conn
command.CommandText = sql0
command1.Connection = conn
command1.CommandText = sql1
command2.Connection = conn
command2.CommandText = sql2
adapter.SelectCommand = command
myadapter.SelectCommand = command1
myadapter2.SelectCommand = command2
adapter.Fill(dt)
myadapter.Fill(dt1)
myadapter2.Fill(dt2)
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "cname"
ComboBox2.DataSource = dt1
ComboBox2.DisplayMember = "dname"
ComboBox3.DataSource = dt2
ComboBox3.DisplayMember = "ename"
conn.Close()
conn.Dispose()
This is the insert statement on button click event..
Try
sql = "insert into invoice(transactionid, description, quantity, weight, agent, airline, destination, price,time) values('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox3.Text & "', '" & TextBox4.Text & "', '" & ComboBox1.SelectedItem & "', '" & ComboBox2.Selecteditem & "', '" & ComboBox3.Selecteditem & "', '" & TextBox5.Text & "', NOW());"
command.Connection = conn
command.CommandText = sql
command.ExecuteNonQuery()
MsgBox("operation successfull")
Catch myerror As MySqlException
MsgBox("operation failed")
End Try
this is the error msg
Operator '&' is not defined for string "insert into invoice(transactioni" and type 'DataRowView'.

Report
Re: need help!! Posted by seancampbell on 19 Nov 2009 at 12:00 PM
Sorry for my delay in responding...

It seems to me that your problem isn't a SQL error, but instead a syntax error.

.SelectedItem is an Object (in this case, a DataViewRow, because it is bound from a datatable). You cannot convert a DataViewRow into a string value... What you need to use is .SelectedValue or .SelectedItem.Value

So change the sql= line to this:
sql = "insert into invoice(transactionid, description, quantity, weight, agent, airline, destination, price,time) values('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox3.Text & "', '" & TextBox4.Text & "', '" & ComboBox1.SelectedItem.Value & "', '" & ComboBox2.SelectedItem.Value & "', '" & ComboBox3.SelectedItem.Value & "', '" & TextBox5.Text & "', NOW());"
Report
Re: need help!! Posted by azdonald on 20 Nov 2009 at 8:03 AM
Thanks Sean for all your help.. I have tried the suggestion you gave me but they don't seem to work. I'm using vb.net express 2008 and mysql server 5.1. However when i use .selectedIndex or .SelectedText, the app doesnt crash...but fails to insert.
Besides intellisense does not give me the .SelectedItem.value option.

By the way have you ever worked with winwedge???
Thanks for all your time.
Report
Re: need help!! Posted by seancampbell on 22 Nov 2009 at 9:26 AM
try SelectedItem.Text
or
SelectedText
possibly
SelectedValue?

Is this a Blue Squiggly Underline error (syntax error that wont let you compile) or is this a Run-Time error (the program runs, but crashes when it hits that line of code)?
Report
Re: need help!! Posted by azdonald on 23 Nov 2009 at 12:15 AM
When i use selectedText, the program runs but fails to insert the values into the database.
selectedvalue and selecteditem crashes the program because they are objects and cant be converted to string.
selectedindex works but it inserts the index of the value and not the value itself.
Report
Re: need help!! Posted by azdonald on 23 Nov 2009 at 12:15 AM
When i use selectedText, the program runs but fails to insert the values into the database.
selectedvalue and selecteditem crashes the program because they are objects and cant be converted to string.
selectedindex works but it inserts the index of the value and not the value itself.
Report
Re: need help!! Posted by seancampbell on 1 Dec 2009 at 8:13 AM
I am sorry I didn't catch this sooner... Firstly, I was just on a 2 week vacation, but more importantly, because I wasn't paying enough attention, for that I appologize...

In your second reply, you pasted code... In this part of your code:
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "cname"
ComboBox2.DataSource = dt1
ComboBox2.DisplayMember = "dname"
ComboBox3.DataSource = dt2
ComboBox3.DisplayMember = "ename"


Do this:
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "cname"
ComboBox1.ValueMember = "cname"
ComboBox2.DataSource = dt1
ComboBox2.DisplayMember = "dname"
ComboBox2.ValueMember = "dname"
ComboBox3.DataSource = dt2
ComboBox3.DisplayMember = "ename"
ComboBox3.ValueMember = "ename"


And use SelectedValue
Report
Re: need help!! Posted by azdonald on 2 Dec 2009 at 1:37 AM
Big thanks to you Sean. I'm really grateful. Thanks so much.
Really looking forward to learning from you.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.