By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,778 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,778 IT Pros & Developers. It's quick & easy.

Filling textboxes with results from a stored procedure

P: n/a
Eva
Hi

I have 2 comboboxes on my form that gathers 2 parameters: The Caravan_Name and the caravan_Length. these 2 values that the user selects are placed into a stored procedure as parameters. The stored procedure is run on the CBoxEditCaravan_Length_SelectedIndexChange
What i want to achieve is to fill the textboxes that are on my form with the results of this query. can someone tell me how i can do this?? I have tried the code below but i keep getting a message that pops up stating ....
cast from string "Length" to type 'integer' is not valid
this message appears everytime i try to change the caravan_length in the combobox

here is my code i tried

'Display the caravan length based on the caravan name selected
Dim Cn As New SqlConnection("data source=PARAGLAPTOP\VSdotNET;initial catalog=CaravanDBLapSQLServ;integrated security=SSPI;persist security info=False;workstation id=PARAGLAPTOP;packet size=4096"

Dim cmdCaraDetails As New SqlCommand("SPSelCaraDetailsOnCaraNameLength", Cn
cmdCaraDetails.CommandType = CommandType.StoredProcedur

'declare the parameter
cmdCaraDetails.Parameters.Add("@CaraName", SqlDbType.Char, 50
cmdCaraDetails.Parameters.Add("@Length", SqlDbType.Char, 10
'now set the value
cmdCaraDetails.Parameters("@CaraName").Value = CBoxEditCaraName.SelectedIte
cmdCaraDetails.Parameters("@Length").Value = CBoxEditCaraLength.SelectedIte

Tr
Cn.Open(
Dim reader5 As SqlDataReade
reader5 = cmdCaraDetails.ExecuteReader(
While reader5.Rea
txtLength.Text = (reader5.GetString("Length")
txtCaraName.Text = (reader5.GetString("Caravan_Model")
txtBeds.Text = (reader5.GetInt16("Beds")
txtPrice.Text = (reader5.GetSqlMoney("Cost")
End Whil
Catch ex As Exceptio
MsgBox(ex.Message
Finall
Cn.Close(
End Tr

the following line (txtPrice.Text = (reader5.GetSqlMoney("Cost"))
in the code above is also highlighted with a message stating...
Value of type 'System.Data.Sqltypes.SqlMoney' cannot be converted to 'String'

can someone plz tell me what i am doing wrong
Can i use the sqldatareader to place single values into a textbox? Im not to sure if this is the right way to go about this??
Nov 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
APG
Hi Eva,

Converting the SqlMoney value to string before assigning it to the TextBox
text value will solve the cast error.
ie., txtPrice.Text = reader5.GetSqlMoney("Cost").ToString()

Also check the data type of the "Length" field in the DB. looks like it is
defined as an integer field.

HTH,

APG
"Eva" <an*******@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
Hi,

I have 2 comboboxes on my form that gathers 2 parameters: The Caravan_Name and the caravan_Length. these 2 values that the user selects are placed into
a stored procedure as parameters. The stored procedure is run on the
CBoxEditCaravan_Length_SelectedIndexChanged What i want to achieve is to fill the textboxes that are on my form with the results of this query. can someone tell me how i can do this?? I have
tried the code below but i keep getting a message that pops up stating ..... cast from string "Length" to type 'integer' is not valid.
this message appears everytime i try to change the caravan_length in the combobox.
here is my code i tried.

'Display the caravan length based on the caravan name selected.
Dim Cn As New SqlConnection("data source=PARAGLAPTOP\VSdotNET;initial catalog=CaravanDBLapSQLServ;integrated security=SSPI;persist security
info=False;workstation id=PARAGLAPTOP;packet size=4096")
Dim cmdCaraDetails As New SqlCommand("SPSelCaraDetailsOnCaraNameLength", Cn) cmdCaraDetails.CommandType = CommandType.StoredProcedure

'declare the parameters
cmdCaraDetails.Parameters.Add("@CaraName", SqlDbType.Char, 50)
cmdCaraDetails.Parameters.Add("@Length", SqlDbType.Char, 10)
'now set the values
cmdCaraDetails.Parameters("@CaraName").Value = CBoxEditCaraName.SelectedItem cmdCaraDetails.Parameters("@Length").Value = CBoxEditCaraLength.SelectedItem
Try
Cn.Open()
Dim reader5 As SqlDataReader
reader5 = cmdCaraDetails.ExecuteReader()
While reader5.Read
txtLength.Text = (reader5.GetString("Length"))
txtCaraName.Text = (reader5.GetString("Caravan_Model"))
txtBeds.Text = (reader5.GetInt16("Beds"))
txtPrice.Text = (reader5.GetSqlMoney("Cost"))
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
Cn.Close()
End Try
the following line (txtPrice.Text = (reader5.GetSqlMoney("Cost")))
in the code above is also highlighted with a message stating....
Value of type 'System.Data.Sqltypes.SqlMoney' cannot be converted to 'String'.
can someone plz tell me what i am doing wrong?
Can i use the sqldatareader to place single values into a textbox? Im not

to sure if this is the right way to go about this??
Nov 22 '05 #2

P: n/a
APG
Hi Eva,

Converting the SqlMoney value to string before assigning it to the TextBox
text value will solve the cast error.
ie., txtPrice.Text = reader5.GetSqlMoney("Cost").ToString()

Also check the data type of the "Length" field in the DB. looks like it is
defined as an integer field.

HTH,

APG
"Eva" <an*******@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
Hi,

I have 2 comboboxes on my form that gathers 2 parameters: The Caravan_Name and the caravan_Length. these 2 values that the user selects are placed into
a stored procedure as parameters. The stored procedure is run on the
CBoxEditCaravan_Length_SelectedIndexChanged What i want to achieve is to fill the textboxes that are on my form with the results of this query. can someone tell me how i can do this?? I have
tried the code below but i keep getting a message that pops up stating ..... cast from string "Length" to type 'integer' is not valid.
this message appears everytime i try to change the caravan_length in the combobox.
here is my code i tried.

'Display the caravan length based on the caravan name selected.
Dim Cn As New SqlConnection("data source=PARAGLAPTOP\VSdotNET;initial catalog=CaravanDBLapSQLServ;integrated security=SSPI;persist security
info=False;workstation id=PARAGLAPTOP;packet size=4096")
Dim cmdCaraDetails As New SqlCommand("SPSelCaraDetailsOnCaraNameLength", Cn) cmdCaraDetails.CommandType = CommandType.StoredProcedure

'declare the parameters
cmdCaraDetails.Parameters.Add("@CaraName", SqlDbType.Char, 50)
cmdCaraDetails.Parameters.Add("@Length", SqlDbType.Char, 10)
'now set the values
cmdCaraDetails.Parameters("@CaraName").Value = CBoxEditCaraName.SelectedItem cmdCaraDetails.Parameters("@Length").Value = CBoxEditCaraLength.SelectedItem
Try
Cn.Open()
Dim reader5 As SqlDataReader
reader5 = cmdCaraDetails.ExecuteReader()
While reader5.Read
txtLength.Text = (reader5.GetString("Length"))
txtCaraName.Text = (reader5.GetString("Caravan_Model"))
txtBeds.Text = (reader5.GetInt16("Beds"))
txtPrice.Text = (reader5.GetSqlMoney("Cost"))
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
Cn.Close()
End Try
the following line (txtPrice.Text = (reader5.GetSqlMoney("Cost")))
in the code above is also highlighted with a message stating....
Value of type 'System.Data.Sqltypes.SqlMoney' cannot be converted to 'String'.
can someone plz tell me what i am doing wrong?
Can i use the sqldatareader to place single values into a textbox? Im not

to sure if this is the right way to go about this??
Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.