为什么我无法在ASPNet.aspx中的HTML表格中显示数据库中的数据

时间:2015-03-19 09:16:22

标签: c# html asp.net sql-server

我正在尝试将一些数据从SQL服务器显示到ASPNet中的HTML表,这是我目前的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Configuration;
using System.Data;


namespace WebApplication17
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
       {
        if (!this.IsPostBack)
        {
            //Populating a DataTable from database.
            DataTable dt = this.GetData();

            //Building an HTML string.
            StringBuilder html = new StringBuilder();

            //Table start.
            html.Append("<table border = '1'>");

            //Building the Header row.
            html.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                html.Append("<th>");
                html.Append(column.ColumnName);
                html.Append("</th>");
            }
            html.Append("</tr>");

            //Building the Data rows.
            foreach (DataRow row in dt.Rows)
            {
                html.Append("<tr>");
                foreach (DataColumn column in dt.Columns)
                {
                    html.Append("<td>");
                    html.Append(row[column.ColumnName]);
                    html.Append("</td>");
                }
                html.Append("</tr>");
            }

            //Table end.
            html.Append("</table>");

            //Append the HTML string to Placeholder.
            PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });
        }
    }


    private DataTable GetData()
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT id, Nome, Cognome FROM Anagrafica"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
               }
            }
        }
   }
}

这是我到目前为止的HTML标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication17.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <title></title>
</head>
<body>
        <asp:PlaceHolder ID = "PlaceHolder1" runat="server" />
</body>
</html>

问题是:我继续在constr = ConfigurationManager.ConnectionStrings [“constr”]上获取NullReferenceException错误.ConnectionString;

我无法解决它...请问任何想法?谢谢你的建议。

2 个答案:

答案 0 :(得分:1)

好的,我找到了一个有效的解决方案:

private DataTable GetData()
    {

        SqlConnection con = new SqlConnection("Data Source=NB465\\SQLEXPRESS;Initial Catalog=Movie;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT id, Nome, Cognome FROM Anagrafica");
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        DataTable dt = new DataTable();
        sda.Fill(dt);
        return dt;
    }

答案 1 :(得分:0)

您需要在configuration节点中的Web.Config文件中添加连接字符串: -

Working with Configuration file & Connection Strings

<configuration>
    <connectionStrings>
      <add name="constr" 
       providerName="System.Data.SqlClient" 
       connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=
             |DataDirectory|\Database1.mdf;Integrated Security=True;" />
    </connectionStrings>
  </configuration>

这些是基础知识,你应该参考一些来自MSDN的tutorails。