Assing sql query to gridview

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 4
Reputation: speranza is an unknown quantity at this point 
Solved Threads: 0
speranza speranza is offline Offline
Newbie Poster

Assing sql query to gridview

 
0
  #1
Nov 15th, 2008
can you help me assign sql query to gridview..i am trying like this but it doesnt work.


  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

  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>
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 32
Reputation: Jihad is an unknown quantity at this point 
Solved Threads: 1
Jihad Jihad is offline Offline
Light Poster

Re: Assing sql query to gridview

 
0
  #2
Nov 15th, 2008
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.
  1. GridView1.Databind()
and don't forget to close the data reader
Last edited by Jihad; Nov 15th, 2008 at 1:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 32
Reputation: reena12 is an unknown quantity at this point 
Solved Threads: 4
reena12 reena12 is offline Offline
Light Poster

Re: Assing sql query to gridview

 
0
  #3
Nov 17th, 2008
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
Reena Mehta

Remember to click on Mark as answer on the post that helped

http://www.indianic.com
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC