在这个程序中,我得到以下错误:
GridView1 的
DataSourceID
必须是IDataSource类型的控件的ID。找不到ID为SqlDataSource1
的控件。
Imports MySql.Data.MySqlClient
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim con As MySqlConnection = New MySqlConnection("data source=localhost;database=dbconnect;user id=root;password=search;")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btn_display_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_display.Click
Dim connection As MySqlConnection = New MySqlConnection("data source=localhost;database=dbconnect;user id=root;password=search;")
Dim mydataset As New DataSet()
Dim mydataadpter As MySqlDataAdapter = New MySqlDataAdapter()
Dim mysql As MySqlCommand = New MySqlCommand("select * from userinfo", connection)
connection.Open()
mydataadpter.SelectCommand = mysql
mydataadpter.Fill(mydataset, "product")
Try
'GridView1.DataSource = mydataset
GridView1.DataBind()
GridView1.DataMember = "product"
connection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
答案 0 :(得分:1)
问题是DataSourceID属性被设计为在ASPX页面中使用,并且您在代码中。您想要使用DataSource属性。它在您的ASPX页面中查找名为“SqlDataSource1”的控件,但没有一个。如果在ASPX页面而不是代码中创建此SqlDataSource1对象,则可以将DataSourceID设置为SqlDataSource1对象的ID
DataSourceID="SqlDataSource1"
答案 1 :(得分:1)
从源视图(.aspx)中的GridView1控件中删除DataSourceID属性及其值(DataSourceID="SqlDataSource1"
)。