Pourquoi je ne peux pas afficher datatables de la database dans la table HTML dans ASPNet.aspx

J'essaye d'afficher des données d'un SQLserver à une table HTML dans ASPNet, ceci est mon code jusqu'ici:

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 ssortingng. SsortingngBuilder html = new SsortingngBuilder(); //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 ssortingng to Placeholder. PlaceHolder1.Controls.Add(new Literal { Text = html.ToSsortingng() }); } } private DataTable GetData() { ssortingng constr = ConfigurationManager.ConnectionSsortingngs["constr"].ConnectionSsortingng; 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; } } } } } } } 

et ceci est mon balisage HTML jusqu'à présent:

 <%@ 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> 

Le problème est: Je continue d'get une erreur NullReferenceException sur constr = ConfigurationManager.ConnectionSsortingngs ["constr"]. ConnectionSsortingng;

Je ne peux pas le réparer … des idées s'il vous plaît? Merci pour les conseils.

Vous devez append une string de connection dans votre file Web.Config quelque chose comme ça dans le noeud de configuration : –

Travailler avec le file de configuration et les strings de connection

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

Ce sont les bases, vous devriez vous référer à quelques tutorails de MSDN.

Ok, j'ai trouvé une solution qui fonctionne:

 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; }