When the page loads, the GridView (located in an update panel) should be loaded with my first SQL SELECT
statement.
On the button click, the same GridView will be loaded with new data.
What am i doing wrong? On startup, the GridView doesn't appear.
Public cmd As New SqlCommand()
Public percentp As New SqlCommand()
Public da As New SqlDataAdapter(cmd)
Public conn As New SqlConnection("Data Source=TEST-TEST-TEST01;Initial Catalog=TEST;Integrated Security=True")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
cmd.Connection = conn
conn.Open()
Dim ds As New DataSet
cmd.CommandText = "Select * from test1"
da.Fill(ds)
GridView1.DataSource = ds.Tables(0)
da.FillSchema(ds, SchemaType.Mapped)
conn.Close()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
cmd.Connection = conn
conn.Open()
Dim ds As New DataSet
cmd.CommandText = "SELECT * from test"
da.Fill(ds)
GridView1.DataSource = ds.Tables(0)
da.FillSchema(ds, SchemaType.Mapped)
conn.Close()
TextBox1.Text="aaaaaaaaaaa"
End Sub
End Class
I tried GridView1.DataS开发者_如何学Goource = ds.Tables(0) GridView1.DataBind
You need to call
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind() // this is needed to bind the datasource to GridView
You need to call GridView1.DataBind();
after setting the data source.
You have forgotten to include the GridView1.Databind() command after you retrieve your data from the database. You typically set your data source after you retrieve your data, so your Page_Load method would work better like this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
cmd.Connection = conn
conn.Open()
Dim ds As New DataSet
cmd.CommandText = "Select * from test1"
da.Fill(ds)
conn.Close()
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
End Sub
精彩评论