当我从客户编号中选择一个值时。组合框,它不会在文本框中显示名称。 [英] When I select a value from customer no. Combo box, it does not display name in text box.

查看:81
本文介绍了当我从客户编号中选择一个值时。组合框,它不会在文本框中显示名称。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私有  Sub  Ocmbcno_SelectedIndexChanged( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs)句柄 cmbcno.SelectedIndexChanged 

尝试

如果 cmbcno.SelectedIndex<> -1 然后
Dim Reader As OleDbDataReader
调用 connect()
Dim C2_Qry As String = 从客户中选择*,其中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

结束 如果

Catch es 作为例外

结束 尝试

结束 Sub





我尝试了什么:



当我从组合框中更改值时,会发生数据类型错误匹配错误

< br $>


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

尝试

如果cmbcno.SelectedIndex<> -1然后
Dim Reader As OleDbDataReader
调用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

结束尝试

结束子

解决方案

Quote:

当我从客户编号中选择一个值时。组合框,它不会在文本框中显示名称。



您需要在调试器下运行代码以检测问题所在,因为try-catch块会阻止任何错误消息。 br />
当没有记录与查询匹配时会发生什么?

Wilde猜测

  Dim  C2_Qry  As   String  =   从客户中选择*,其中C_ID =& cmbcno.Text&  ' 
' 此处缺少单引号^以匹配此^





您的代码行为不符合您的预期,或者您不明白为什么!



有一个几乎通用的解决方案:运行代码调试器一步一步,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

有在调试器中没有魔法,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你展示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



Visual Basic / Visual Studio Video教程 - 基本调试 - YouTube [ ^ ]

初学者的Visual Basic .NET编程 - 断点和调试工具 [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。



< pre lang =vb> Dim C2_Qry As String = 从客户中选择*,其中C_ID =& cmbcno.Text& '



你有另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]

我该怎么办?解释没有技术术语的SQL注入? - 信息安全堆栈交换 [ ^ ]


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

解决方案

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

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.

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[^]


这篇关于当我从客户编号中选择一个值时。组合框,它不会在文本框中显示名称。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆