单击按钮时如何在ASP.NET 4.0中的Gridview上显示数据?

时间:2022-06-17 21:01:26
Imports System.Data.Odbc
Imports System.Data
Partial Class VIEW_SALARY_DETAILS
     Inherits System.Web.UI.Page

   Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim cons, query As String
    Dim con As OdbcConnection
    Dim adpt As OdbcDataAdapter
    cons = "dsn=Courier; UID=Courier; PWD=123;"
    con = New OdbcConnection(cons)
    con.Open()
    query = "select * from EMPLOYEE"
    Dim ds As DataSet
    adpt = New OdbcDataAdapter(query, con)
    ds = New DataSet
    adpt.Fill(ds, "Courier")
    GridView1.DataSource = ds.Tables()
    con.Close()
   End Sub
End Class

I wrote the above code but it does not display data. Same thing is possible in VB.NET application. How do we do it for ASP.net 4.0?

我写了上面的代码,但它没有显示数据。在VB.NET应用程序中也可以这样做。我们如何为ASP.net 4.0做到这一点?

2 个解决方案

#1


0  

You'll need to call the DataBind() method. Try this

您需要调用DataBind()方法。尝试这个

GridView1.DataSource = ds.Tables()
DataBind()

#2


0  

You missed a line after

你之后错过了一条线

GridView1.DataSource = ds.Tables[0]   //do some correction here..

GridView1.DataBind();        // add this line

You need to bind the GridView with the Datasource..

您需要将GridView与数据源绑定..

Your code will be

你的代码将是

Imports System.Data.Odbc
Imports System.Data
Partial Class VIEW_SALARY_DETAILS
     Inherits System.Web.UI.Page

   Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim cons, query As String
    Dim con As OdbcConnection
    Dim adpt As OdbcDataAdapter
    cons = "dsn=Courier; UID=Courier; PWD=123;"
    con = New OdbcConnection(cons)
    con.Open()
    query = "select * from EMPLOYEE"
    Dim ds As DataSet
    adpt = New OdbcDataAdapter(query, con)
    ds = New DataSet
    adpt.Fill(ds, "Courier")
    GridView1.DataSource = ds.Tables[0]
    GridView1.DataBind()
    con.Close()
   End Sub
End Class

Ensure that connection or sql string you used should be correct.

确保您使用的连接或sql字符串应该是正确的。

#1


0  

You'll need to call the DataBind() method. Try this

您需要调用DataBind()方法。尝试这个

GridView1.DataSource = ds.Tables()
DataBind()

#2


0  

You missed a line after

你之后错过了一条线

GridView1.DataSource = ds.Tables[0]   //do some correction here..

GridView1.DataBind();        // add this line

You need to bind the GridView with the Datasource..

您需要将GridView与数据源绑定..

Your code will be

你的代码将是

Imports System.Data.Odbc
Imports System.Data
Partial Class VIEW_SALARY_DETAILS
     Inherits System.Web.UI.Page

   Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim cons, query As String
    Dim con As OdbcConnection
    Dim adpt As OdbcDataAdapter
    cons = "dsn=Courier; UID=Courier; PWD=123;"
    con = New OdbcConnection(cons)
    con.Open()
    query = "select * from EMPLOYEE"
    Dim ds As DataSet
    adpt = New OdbcDataAdapter(query, con)
    ds = New DataSet
    adpt.Fill(ds, "Courier")
    GridView1.DataSource = ds.Tables[0]
    GridView1.DataBind()
    con.Close()
   End Sub
End Class

Ensure that connection or sql string you used should be correct.

确保您使用的连接或sql字符串应该是正确的。