2

Hi I am trying to read data from database then put this into a table. rows in the table are created automatically depending on how many rows there are in database. I have done this in php using mysql_fetch_array but cannot seem to do it in asp.net webforms. My idea is to use this query to get the information and store in the labels in the server page and create a table there with coloumns data filled using the labels. Here is my code in the 'page_load' Thank you:

<table>
        <tr>
            <th>Surgery</th>
            <th>PatientID</th>
            <th>Location</th>
        </tr>
        <tr>
            <td>

        <asp:Label ID="Label1" runat="server"></asp:Label>
        </td>
        <td>
        <asp:Label ID="Label2" runat="server"></asp:Label>
        </td>
        <td>

        <asp:Label ID="Label3" runat="server"></asp:Label>
        </td>
        </tr>
        </table>



           string query= "select surgery,patientID, location from details";
            SqlCommand result= new SqlCommand(query, conn);
            result.ExecuteNonQuery();
            using (SqlDataReader getInfo= result.ExecuteReader())


                while (getInfo.Read())
                {
                    Label1.Text = getInfo["surgery"].ToString();
                    Label2.Text = getInfo["patientID"].ToString();
                    Label3.Text = getInfo["location"].ToString();


                }

SqlCommand cmd = new SqlCommand("select surgery, PatientID, location from details", conn); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); conn.Close();

11

1 Answer 1

1

Add GridView in your aspx code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField HeaderText="Surgery" DataField="surgery" />
        <asp:BoundField HeaderText="PatientID" DataField="patientID" />
        <asp:BoundField HeaderText="Location" DataField="location" />
    </Columns>
</asp:GridView>

You C# code is fine, just close the connection before binding to gridview:

SqlCommand cmd = new SqlCommand("select surgery, PatientID, location from details", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable(); 
sda.Fill(dt);

conn.Close();

GridView1.DataSource = dt; 
GridView1.DataBind();
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you :) appreciate the help. to style it would be same as normal tables? am i correct?
yeah, just write a css class and set its property cssClass="youCSSclass"
thankyou. Last question say I would like to add a delete button next to each row. How would I pass the 'id' through the server? I have done this in php and would use the same approach. Add extra colunm and change the DataField in aspx to the 'button_click' and place the code in that functiion in C#? Do I use %button_click% if I would like to use the variable in the aspx page. Thank you
Actually its a bit complex to implement CRUD using GridView than in php. I share an article with you please do not go for just copy paste, read it and understand how it works. Here is it: aspsnippets.com/Articles/…
Hi thank you for that info. Is there no simpler method for the edit and delete?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.