Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Private Sub Ocmbcno_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbcno.SelectedIndexChanged

        Try

            If cmbcno.SelectedIndex <> -1 Then
                Dim Reader As OleDbDataReader
                Call connect()
                Dim C2_Qry As String = "select * from Customer where C_ID=" & cmbcno.Text & " '"

                comm.Connection = conn
                comm.CommandType = CommandType.Text
                comm.CommandText = C2_Qry
                Reader = comm.ExecuteReader

                Reader.Read()
                txtCname.Text = Reader.Item("cname")
                otxtcity.Text = Reader.Item("ccity")
                txtOCB.Text = Reader.Item("cbalance")
                rdr.Close()

                cmbPno.Enabled = True

            End If

        Catch es As Exception

        End Try

    End Sub


What I have tried:

Data type mis match error occur when i change value from combo box


<pre>Private Sub Ocmbcno_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbcno.SelectedIndexChanged

        Try

            If cmbcno.SelectedIndex <> -1 Then
                Dim Reader As OleDbDataReader
                Call connect()
                Dim C2_Qry As String = "select * from Customer where C_ID=" & cmbcno.selectedindex

                comm.Connection = conn
                comm.CommandType = CommandType.Text
                comm.CommandText = C2_Qry
                Reader = comm.ExecuteReader

                Reader.Read()
                txtCname.Text = Reader.Item("cname")
                otxtcity.Text = Reader.Item("ccity")
                txtOCB.Text = Reader.Item("cbalance")
                rdr.Close()

                cmbPno.Enabled = True

            End If

        Catch es As Exception

        End Try

    End Sub
Posted
Updated 18-May-19 19:04pm
v2

1 solution

Quote:
When I select a value from customer no. Combo box, it does not display name in text box.

You need to run your code under debugger to detect where is the problem because the try-catch block prevent any error message.
What happen when no record match the query?
Wilde guess
VB
Dim C2_Qry As String = "select * from Customer where C_ID=" & cmbcno.Text & " '"
' a single quote is missing                          here ^ to match this     ^


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

VB
Dim C2_Qry As String = "select * from Customer where C_ID=" & cmbcno.Text & " '"

Another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
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