【发布时间】:2019-08-02 09:55:59
【问题描述】:
我正在创建一个图书网站,我创建了一个图书页面,当我单击该链接时,我在其中给出了编辑图书页面链接,应根据数据库中的图书 ID 打开更新表单,并且应填写更新表单使用数据库中预先输入的详细信息。
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WEbApp
{
public partial class Edit_Books : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="ConnectionString";Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
int id = Convert.ToInt32(Request.QueryString["bkid"].ToString());
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Books where bkid="+id+"";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach(DataRow dr in dt.Rows)
{
Textbox1.Text = dr["book_name"].ToString();
Textbox2.Text = dr["book_author"].ToString();
Textbox3.Text = dr["book_publication"].ToString();
}
}
}
}
当我运行上面的代码时,我在这一行得到一个错误:
int id = Convert.ToInt32(Request.QueryString["bkid"].ToString());
Object reference not set to an instance of an object.
【问题讨论】:
标签: c# asp.net sql-server database project-management