943,657 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 4033
  • ASP.NET RSS
Nov 15th, 2008
0

Assing sql query to gridview

Expand Post »
can you help me assign sql query to gridview..i am trying like this but it doesnt work.


ASP.NET Syntax (Toggle Plain Text)
  1. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  2. Dim con As New SqlConnection("Data Source=SPERANZA\SQLEXPRESS;initial catalog=master;integrated security=true")
  3. Dim cmd As New SqlCommand
  4. Dim dr As SqlDataReader
  5. cmd.Connection = con
  6. con.Open()
  7. cmd.CommandText = "SELECT name,dob,sex FROM users WHERE name LIKE ('%' + @name + '%')"
  8. cmd.Parameters.AddWithValue("@name", txtname.Text)
  9. dr = cmd.ExecuteReader()
  10. GridView1.DataSource = dr
  11. con.Close()
  12. End Sub


and code in aspx file

ASP.NET Syntax (Toggle Plain Text)
  1.  
  2. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="176px" Width="598px">
  3. <Columns>
  4. <asp:TemplateField>
  5. <ItemTemplate>
  6. <%#Eval("name")%><br />
  7. <%#Eval("dob")%><br />
  8. <%#Eval("sex") %><br />
  9. </ItemTemplate>
  10. </asp:TemplateField>
  11. </Columns>
  12. </asp:GridView>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
speranza is offline Offline
4 posts
since Nov 2008
Nov 15th, 2008
0

Re: Assing sql query to gridview

The only thing you are missing is that you should bind the gridview to its data source. after the set of the data source and before closing the connection write this.
ASP.NET Syntax (Toggle Plain Text)
  1. GridView1.Databind()
and don't forget to close the data reader
Last edited by Jihad; Nov 15th, 2008 at 1:43 pm.
Reputation Points: 10
Solved Threads: 1
Light Poster
Jihad is offline Offline
32 posts
since Nov 2007
Nov 17th, 2008
0

Re: Assing sql query to gridview

Try
Dim ObjSqlDataAdapter As SqlDataAdapter
Dim ObjDataSet As New DataSet
ObjSqlConnection = New SqlConnection(ConnectionString)

If ObjSqlConnection.State <> ConnectionState.Open Then
ObjSqlConnection.Open()
End If

ObjSqlDataAdapter = New SqlDataAdapter(CommandText, ObjSqlConnection)
ObjDataSet = New DataSet()
ObjSqlDataAdapter.Fill(ObjDataSet)
Gridview1.DataSource = ObjDataSet.Tables(0)

Catch ex As Exception
Throw New Exception(ex.Message)
Finally
ObjSqlConnection.Close()
ObjSqlConnection.Dispose()
End Try

if solved pls mark as solved
Reputation Points: 12
Solved Threads: 4
Light Poster
reena12 is offline Offline
32 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: use a querystring value to fetch value from database
Next Thread in ASP.NET Forum Timeline: How do I Evaluate Null value from database?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC