Click here to Skip to main content
15,888,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
ConnectDatabase()
Cmd = New SqlCommand("stockon2", Con)
Cmd.CommandType = CommandType.StoredProcedure
Cmd.Parameters.AddWithValue("@hd", CInt(14572))
Cmd.Parameters("@hd").Direction = ParameterDirection.Input
Cmd.Parameters.AddWithValue("@dt", CInt(Txtdate.Text))
Cmd.Parameters("@dt").Direction = ParameterDirection.Input
Cmd.Parameters.AddWithValue("@rateonly", CInt(txtip.Text))
Cmd.Parameters("@rateonly").Direction = ParameterDirection.Input
Cmd.Parameters.AddWithValue("@qty", CInt(0))
Cmd.Parameters("@qty").Direction = ParameterDirection.Input
Cmd.Parameters.Add("@val", SqlDbType.Float)
Cmd.Parameters("@val").Direction = ParameterDirection.Output
Cmd.ExecuteReader()
TxtValue.Text = Cmd.Parameters("@val").Value
Con.Close()

i got below attached error

Conversion from type 'dbnull to the string is not valid
Posted
Updated 23-Sep-14 3:00am
v3
Comments
[no name] 23-Sep-14 8:49am    
Well it's obvious is it not? Your SP is returning null.
Abdul Samad KP 23-Sep-14 9:05am    
As Wes Aday said , @val is dbnull. To avoid this error use .ToString()

Cmd.ExecuteNonQuery()
TxtValue.Text = Cmd.Parameters("@val").Value.ToString()
PhilLenoir 23-Sep-14 9:10am    
Try running in debug, get the values you are passing and run the query in SQL Management Studio to debug. Best guess, there are no rows being returned, but without seeing your stored proc .....

1 solution

It seems to be obvious - your output parameter (@val) get NULL as value!
If this is an unacceptable result you must check your SP.
If this is an acceptable result change this line:
C#
TxtValue.Text = Cmd.Parameters("@val").Value

to this:
C#
TxtValue.Text = Convert.ToString(Cmd.Parameters("@val").Value)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900