简体   繁体   中英

How to insert data from database into a table using c# asp.net?

I'm fairly new to asp.net and c#, i've connected to a SQL database and now i'd like to show the data i have into a table.

This is my back-end:

public string getWhileLoopData()
{
    string htmlStr = "";
    SqlConnection conn = new SqlConnection("Data Source = secret;Initial Catalog = GTI;Persist Security Info = True;Integrated Security = true;User ID = user;Password = pass;");
    conn.Open();
    SqlCommand cmd = new SqlCommand("SELECT * FROM [CORE_SYS_STATUS]", conn);
    SqlDataReader reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        int ID = reader.GetInt32(0);
        int SYSTEM_NAME = reader.GetInt32(0);
        int SYSTEM_STATUS = reader.GetInt32(0);
        int SYSTEM_SHORTMSG = reader.GetInt32(0);

        htmlStr += "<tr><td>" + ID + "<tr><td>" + SYSTEM_NAME + "<tr><td>" + SYSTEM_STATUS + "<tr><td>" + SYSTEM_SHORTMSG;
    }

    conn.Close();
    return htmlStr;
}

This is my front-end:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="ContentPlaceHolder">

    <div class="bg-light text-center bg-light rounded border border-dark m-4">

        <div class="col-md-12">
            <h1 class="display-4 text-center p-4">Gestão de Alertas</h1>

            <table class="table table-bordered table-hover text-center p-4 border border-dark">
                <thead>
                    <tr class="table-success disabled">
                        <th style="width: 5%" scope="col">ID</th>
                        <th style="width: 20%" scope="col">Nome</th>
                        <th style="width: 15%" scope="col">Status</th>
                        <th style="width: 45%" scope="col">Mensagem</th>
                    </tr>
                </thead>
                <tbody>
                   <!-- I want to insert data here -->
                </tbody>
            </table>
        </div>
    </div>
</asp:Content>

And this is the result: Result

It may look really silly and easy but i'm very new to this of programming, if anyone could help me figure out how to insert my data into the table i'd be very glad. Thank you!

There are many ways to achieve this.

I would recommend to take a look in the docs regarding to DataBinding, eg:

Retrieving data in aspnet web-forms

GridView DataBind (look at the sample there)

Furthermore you should take a look at the documentation regarding GetInt32: GetInt32 MSDN

The parameter is the index of the column in the select statement and you are always passing 0 and that is certainly not what you want. I'd also recommend to explicitly name the columns you want in the select statement instead of using select *.

For a quick solution you could replace <%=getWhileLoopData()%> with an <asp:Literal /> control; in your code-behind, set it's Text property to (getWhileLoopData).

If you're new to ASP.NET, learning WebForms data binding will take a while as it's got some rules and peculiarities you need to get to grips with. To be honest unless you have to build this thing in WebForms you should start learning ASP.NET MVC, WebForms is a dead technology.

<% HtmlString str = new HtmlString(getWhileLoopData()); %>
<%= str %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM