| | |
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 |
.net 2.0 activexcontrol advice ajax alltypeofvideos asp asp.net bc30451 bottomasp.net browser businesslogiclayer button c# c#gridviewcolumn checkbox child click commonfunctions compatible confirmationcodegeneration content contenttype courier css dataaccesslayer database datagrid datagridview datagridviewcheckbox datalist deadlock development dgv dropdownlist dropdownmenu edit expose feedback flash flv form formatdecimal forms formview gridview homeedition iframe iis javascript jquery listbox login menu microsoft mono mouse mssql multistepregistration news numerical objects order panelmasterpagebuttoncontrols parent radio ratings reportemail rotatepage save schoolproject search security serializesmo.table silverlight smartcard software sql-server sqlserver2005 suse textbox tracking typeof unauthorized validation vb.net video videos virtualdirectory vista visual-studio visualstudio web webapplications webarchitecture webdevelopemnt webprogramming webservice xml youareanotmemberofthedebuggerusers





