我想爲此數據庫進行搜索按鈕。我不確定我在這裏做錯了什麼。它給我的錯誤是System.NullReferenceException:未將對象引用設置爲對象的實例。這是關於字符串constring ..等,這是它突出顯示它紅色,反正。從asp.net中的訪問數據庫嘗試在gridview中的搜索按鈕不工作
所以,我讀了一篇關於訪問字符串連接的文章。我做了它所說的。我只是沒有看到我做錯了什麼。
代碼:
public partial class StudentView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
this.BindGrid();
}
private void BindGrid()
{
string path = "C:\\Users\\Andrew James Heid\\Desktop\\12\\App_Code\\University DB.accdb";
string conString = ConfigurationManager.ConnectionStrings["Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path].ConnectionString;
string query = "SELECT * FROM Students";
string id = TextBox1.Text.Trim();
if (!string.IsNullOrEmpty(id))
{
id = string.Format(" where ID = '{0}'", id);
}
SqlCommand cmd = new SqlCommand(query + id);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
}
你從哪裏得到錯誤? –
'.ConnectionStrings'需要連接字符串名稱或索引。 –
@Hanlet什麼是一個例子? –