| | |
Assing sql query to gridview
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 4
Reputation:
Solved Threads: 0
can you help me assign sql query to gridview..i am trying like this but it doesnt work.
and code in aspx file
ASP.NET Syntax (Toggle Plain Text)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim con As New SqlConnection("Data Source=SPERANZA\SQLEXPRESS;initial catalog=master;integrated security=true") Dim cmd As New SqlCommand Dim dr As SqlDataReader cmd.Connection = con con.Open() cmd.CommandText = "SELECT name,dob,sex FROM users WHERE name LIKE ('%' + @name + '%')" cmd.Parameters.AddWithValue("@name", txtname.Text) dr = cmd.ExecuteReader() GridView1.DataSource = dr con.Close() End Sub
and code in aspx file
ASP.NET Syntax (Toggle Plain Text)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="176px" Width="598px"> <Columns> <asp:TemplateField> <ItemTemplate> <%#Eval("name")%><br /> <%#Eval("dob")%><br /> <%#Eval("sex") %><br /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
•
•
Join Date: Nov 2007
Posts: 32
Reputation:
Solved Threads: 1
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.
and don't forget to close the data reader
ASP.NET Syntax (Toggle Plain Text)
GridView1.Databind()
Last edited by Jihad; Nov 15th, 2008 at 1:43 pm.
•
•
Join Date: Oct 2008
Posts: 32
Reputation:
Solved Threads: 4
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
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
![]() |
Other Threads in the ASP.NET Forum
- Previous Thread: use a querystring value to fetch value from database
- Next Thread: How do I Evaluate Null value from database?
| Thread Tools | Search this Thread |
Tag cloud for ASP.NET
.net 2.0 3.5 ajax appliances application asp asp.net beginner box browser businesslogiclayer button c# cac checkbox child class compatible complex content contenttype control countryselector courier database datagrid datagridview datalist deployment development dgv dialog dropdown dropdownmenu dynamic dynamically edit editing embeddingactivexcontrol feedback fileuploader fill findcontrol flash flv folder form gridview gudi iis image javascript languages list maps menu mobile mssql nameisnotdeclared novell opera order problem profile ratings redirect refer registration relationaldatabases reportemail response.redirect rows search security select serializesmo.table sessionvariables silverlight smoobjects software sql ssl tracking treeview typeof validatedate validation vb.net vista visual-studio visualstudio vs2008 web webapplications webarchitecture webdevelopment webprogramming wizard xsl





