Gridview Datasource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • speranza
    New Member
    • Nov 2008
    • 3

    Gridview Datasource

    Hi,
    İ dont know how to assign sql command query value to a gridview..i am trying like this..it dosnt work can you help me?

    Code:
    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 the aspx file

    Code:
    <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>
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Although you have listed the datasource, I believe that you still need a databind.

    Comment

    Working...