0
嘿我試圖添加數據庫信息到我的網頁。這是我正在嘗試的代碼。如何將數據庫內容添加到一個asp.net頁面
using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
namespace DatabaseAddDemo
{
public partial class Content : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["key"] != null)
{
try
{
SqlConnection sqlConn = new SqlConnection(@"Data Source=officedev1;Initial Catalog=TestDatabase;User ID=sa;Password=Password11;pooling='true';Connect Timeout=3000; Max Pool Size=200;MultipleActiveResultSets='true'");
SqlCommand cmdPreWork = new SqlCommand(@"select * from CKEditor_Table where @ID = @key", sqlConn);
string key = Request.QueryString["keys"].ToString();
contentLiteral.Text = key;
cmdPreWork.Parameters.Add("@Information", SqlDbType.Char).Value = key;
Console.WriteLine(contentLiteral);
SqlDataAdapter daPreWork = new SqlDataAdapter(cmdPreWork);
DataTable dtPreWork = new DataTable();
daPreWork.Fill(dtPreWork);
Grid.DataSource = dtPreWork;
Grid.DataBind();
}
catch (Exception ex)
{
lblError.Text = "Could not open connection";
}
}
}
}
}
每當我試圖顯示的信息,我得到的lblError文本,告訴我,我無法打開連接。我不知道該怎麼做。請幫忙。
請張貼在catch塊,你得到的異常 – Viru
步驟通過與調試器的代碼,並找出異常被拋出什麼。我已經可以看到你沒有在你的'SqlConnection'上調用'Open()',但可能還有其他錯誤。 – Slappywag
這是一個空引用異常。 –