Karim Tarek
Karim Tarek

Reputation: 401

Putting the output of a SQL query in a table

I just cant figure out how to get the output of a query and be able to manipulate it, meaning that i have a database and i have a table named CoursePages that contain all the coursePages in my university, so when a student or somebody wants to subscribe to a course page i want to view all the records in the table(coursePages) and make them clickable just in order to know which one he/she pressed and save it. So is there any way to generate such thing ?

Here is my code :

protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=JASONTT-PC\\SQLSERVER;Initial Catalog=GNetwork;Integrated Security=True";
    SqlCommand comm = new SqlCommand();
    comm.Connection = conn;
    comm.Connection.Open();
    String query = "SELECT * FROM Course_Pages ";
    comm.CommandText = query;
    SqlDataReader reader = comm.ExecuteReader();
   // i don't know what to do after i executed the reader .. 
}

maybe like a gridView or sthg ... i just want for every record i will take the name (String ) and put it in a checkbox so that i can tell which ones the user clicked

Upvotes: 0

Views: 984

Answers (1)

Adam Rackis
Adam Rackis

Reputation: 83358

while (reader.Read()){
    //now you can get any values by column index like
    int i = reader.GetInt32(0);
    string s = reader.GetString(1);
    double d = reader.GetInt32(2);
    DateTime dt = reader.GetDateTime(3);
}

If you're asking how to translate these results into links on your web page, you should look at creating a gridview and Binding your reader to it.

Here's a sample:

    using (SqlDataReader reader = comm.ExecuteReader()) {
        gv1.DataSource = reader;
        gv1.DataBind();
    }

    <asp:GridView runat="server" AutoGenerateColumns="false" ID="gv1">
        <Columns>
            <asp:BoundField DataField="Title" HeaderText="Title" />
            <asp:TemplateField>
                <ItemTemplate>
                    <a href='foo.aspx?id=<%# Eval("id") %>'>Link</a>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Finally, note that if you have a field in your returned query that represents the url in toto, you can use a HyperLinkField

<asp:HyperLinkField DataNavigateUrlFields="linkHref" Text="BoundLink" />

DataNavigateUrlFields is the field from your results you're binding the url to.

Upvotes: 1

Related Questions