Click to See Complete Forum and Search --> : sql/vb.net problem


Bantam
March 5th, 2003, 05:15 PM
Whenever I try to programatically execute:

"INSERT INTO tblCampRegistrations(fldRWUID, fldRegionID, fldYear, fldRWUIDDiscount) VALUES(" & strBlah & " , " & cmbRegion.SelectedIndex + 1 & " , " & cmbYear.SelectedItem & ")"

it throws an exception, the .tostring is:


System.InvalidCastException: Cast from type 'DataRowView' to type 'String' is not valid.
at Microsoft.VisualBasic.CompilerServices.StringType.FromObject(Object Value)
at Microsoft.VisualBasic.CompilerServices.ObjectType.StrCatObj(Object vLeft, Object vRight)
at RWUDatabase.frmRegister.cmdOK_Click(Object sender, EventArgs e)



I've taken out every single SQL line that's not required and I still get this, and the datatypes all seem to be ok. Any suggestions?

-Jacob the n00b

DdH
March 6th, 2003, 12:17 AM
I see that you want to insert 4 fields
fldRWUID,
fldRegionID,
fldYear,
fldRWUIDDiscount

But you give only 3 values
strBlah
cmbRegion.SelectedIndex + 1
cmbYear.SelectedItem

The number of fields has to match the number of values.

Bantam
March 6th, 2003, 07:49 AM
Observant, but not the cause of the error. Now the code is:

"INSERT INTO tblCampRegistrations(fldRWUID, fldRegionID, fldYear) " & _
"VALUES(" & strBlah & " , " & _
cmbRegion.SelectedIndex + 1 & " , " & _ cmbYear.SelectedItem & ")"

gknierim
March 7th, 2003, 08:57 AM
When specifying values in an INSERT statement, if the value is a string, you need to enclose it quotes. Also, if you are pulling values from a combobox, then you need to use the .text property to retrieve the text that is selected.

If fldRWUID is a string use quotes, if not - dont. I cant tell but your variable strBlah tells me its a string.

Try this:

Dim sSQL as string
sSQL = "INSERT INTO tblCampRegistrations (fldRWUID, fldRegionID, fldYear) " _
& "VALUES ('" & strBlah & "', " & cmbRegion.SelectedIndex + 1 _
& ", " & cmbYear.SelectedItem.Text & ")"


HTH,
Greg

Bantam
March 7th, 2003, 09:19 AM
Yeah that did it. I checked out what the .selectedmember actually was. I'm an idiot. I got it working last night though, thanks for all the help y'all

-Jacob