问题描述
大家好!
这是编程的新手,我正在其中编写我的第一个程序,我想从sql检索数据并显示在ComboBox中,但是当我尝试运行Page_Load时却得到了(InvalidCastException未处理),有人可以看到我的问题吗? /p>
感谢帮助
================================================ ==============================
导入System.Data.SqlClient
公共类Component_Removal
昏暗的SqlConn作为SqlConnection
将SqlCmd变暗为SqlCommand
昏暗计数为整数
昏暗查询为字符串
昏暗的读者作为SqlDataReader
私有子Component_Removal_Load(作为对象发送,作为EventArgs发送)处理MyBase.Load
SqlConn =新的SqlConnection
SqlConn.ConnectionString =
"服务器= DESKTOP-8HE6EFV \ SQLEXPRESS;数据库= Keyn_Electronics;用户= Keyn; Pwd = keyn161;"
昏暗的阅读器作为SqlDataReader
试试
SqlConn.Open()
查询="SELECT * FROM Radio_Manufacturers"
SqlCmd =新的SqlCommand(Query,SqlConn)
Reader = SqlCmd.ExecuteReader
在Reader上阅读
昏暗的sName = Reader.GetString("numbers")
ComboBox2.Items.Add(sName)
结束时
SqlConn.Close()
捕获为SqlException
MessageBox.Show(ex.Message)
终于
SqlConn.Dispose()
结束尝试
结束
Hi everyone!
Completely new to programming, I am writing my first program in it I want to retrieve data from sql and display in ComboBox, but I get (InvalidCastException was unhandled) when I try to run my Page_Load can anyone see my problem?
Thanks for help
==============================================================================
Imports System.Data.SqlClient
Public Class Component_Removal
Dim SqlConn As SqlConnection
Dim SqlCmd As SqlCommand
Dim Count As Integer
Dim Query As String
Dim Reader As SqlDataReader
Private Sub Component_Removal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SqlConn = New SqlConnection
SqlConn.ConnectionString =
"Server=DESKTOP-8HE6EFV\SQLEXPRESS;Database=Keyn_Electronics;user=Keyn;Pwd=keyn161;"
Dim Reader As SqlDataReader
Try
SqlConn.Open()
Query = " SELECT * FROM Radio_Manufacturers"
SqlCmd = New SqlCommand(Query, SqlConn)
Reader = SqlCmd.ExecuteReader
While Reader.Read
Dim sName = Reader.GetString("numbers")
ComboBox2.Items.Add(sName)
End While
SqlConn.Close()
Catch ex As SqlException
MessageBox.Show(ex.Message)
Finally
SqlConn.Dispose()
End Try
End Sub
这篇关于未处理InvalidCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!