Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to dispaly Item Name in Text Box as per the selection of Combo box.I am using following code:

VB
Dim cmd As New SqlCommand()
    Dim con As New SqlConnection
    Dim rd As SqlDataReader
Dim ds As New DataSet()

        ds = GetView(StrQuery) 'GetView function retrive query result from database
        If (ds.Tables(0).Rows.Count > 0) Then
            cmb.DataSource = ds.Tables(0).DefaultView
            cmb.DisplayMember = Mdisp
            cmb.ValueMember = Mvalue
        End If

Private Sub cmbItemName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbItemName.SelectedIndexChanged

Dim str As String = (" SELECT Item_name FROM Item_Master where Item_no'"=cmbItemName.SelectedValue.ToString()& "'")
        con = obj.SetCon()
        cmd.CommandText = str
        cmd.Connection = con
        rd = cmd.ExecuteReader

        If rd.Read = True Then
            txtItem_code.Text = rd("Item_no")
        End If

        con.Close()

System throws an error at If rd.Read = True Then statement (Error converting data type varchar to numeric.)

I am unable to understand the error.

Please suggest solution.

Thanks in Advance.
Posted
Updated 3-Dec-12 6:57am
v2
Comments
__TR__ 3-Dec-12 13:01pm    
Looks like the SQL query is not formed properly in your code. Best way to see this would be to debug your code and get the value of the string "str" and check how the query is formed.

Your sql string is messed up. It needs to look more like this, but my second code snippet is how this should be done (as per MSDN[^]). The way you have written it leaves the query extremely vulnerable to sql injection.
" SELECT Item_name FROM Item_Master where Item_no = '" & cmbItemName.SelectedValue.ToString()& "'"
VB
Dim commandText As String = "SELECT Item_name FROM Item_Master where Item_no = '@selectedItemNo'"

Using connection As New SqlConnection(connectionString)
   Dim command As New SqlCommand(commandText, connection)

   ' Add selectedItemNo parameter for WHERE clause.
   command.Parameters.AddWithValue("@selectedItemNo", cmbItemName.SelectedValue.ToString())

   Try
      connection.Open()
      rd = command.ExecuteReader

      If rd.Read = True Then
         txtItem_code.Text = rd("Item_no")
      End If
   Catch ex As Exception
      Console.WriteLine(ex.Message)
   End Try 
End Using 
 
Share this answer
 
Thanks.

as per your suggestion I am using following code:
VB
Dim str As String = "SELECT Item_name FROM Item_Master where Item_no = '@selectedItemNo'"
        con = obj.SetCon()
        Dim command As New SqlCommand(str, con)
        command.Parameters.AddWithValue("@selectedItemNo", cmbItemName.SelectedValue.ToString())
Try
            con.Open()
            rd = command.ExecuteReader
If rd.Read = True Then
                txtItem_code.Text = rd("Item_no")
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        con.Close()

I debugging the code after reading rd = command.ExecuteReader statement system directly go to catch statement & show message "The connection was not closed. The connection's current state is open.".
I am unable to find out a problem.

Please help
 
Share this answer
 
Now problem mentioned in above is solved but main problem is still there i.e
System throws an error at If rd.Read = True Then statement (Error converting data type varchar to numeric.)
 
Share this answer
 
Comments
Yogi ,Pune 5-Dec-12 10:32am    
Please suggest solution for my question
DinoRondelly 5-Dec-12 10:45am    
txtItem_code.Text = rd("Item_no").ToString()
Yogi ,Pune 5-Dec-12 11:00am    
on rd = command.ExecuteReader statement system directly go to catch statement & show message "The connection was not closed. The connection's current state is open.".
above mentioned issue still exist.
DinoRondelly 5-Dec-12 11:08am    
So you are still getting the error - If rd.Read = True Then statement (Error converting data type varchar to numeric.) and you are having an issue with your connection?
Yogi ,Pune 6-Dec-12 8:10am    
Yes issue is still there.

Please suggest me a solution
Please note-

You are returning 'Item_name' from your select query while trying to access 'Item_no' in line 'txtItem_code.Text = rd("Item_no")'. Please verify there is some mistake in your code.

Please, Mark as answer if helepd.
 
Share this answer
 
Thanks for your suggestion.
I recheck my code as per your suggestion.But unable to find the problem.I used following table.Can you suggest me at which point
my code is wrong.

SQL
Using connection As New SqlConnection("Data Source=RAKESH\SQLEXPRESS;Initial Catalog=PO;Integrated Security =True")
            Dim str As String = "SELECT Item_name FROM Item_Master where Item_no = '@selectedItemNo'"


VB
Dim command As New SqlCommand(str, connection)
            connection.Open()
            command.Parameters.AddWithValue("@selectedItemNo", cmbItemName.SelectedValue.ToString())

VB
Try
rd = command.ExecuteReader

VB
If rd.Read = True Then
                    txtItem_code.Text = rd("Item_no")
                End If
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
           con.Close()
        End Using
 
Share this answer
 
Comments
Member 12301874 7-Sep-16 5:15am    
1 from to 2 form entry to textbox to Convert comobox out put Entery
after rd = command.ExecuteReader statement system directly go to catch statement & show message "The connection was not closed. The connection's current state is open.".

another error occured is converting data type varchar to numeric after same statement
 
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