我用VS中的GirdView控件做删除信息的功能,我是通过取到鼠标所点到行的ID值,来找到要删除的信息,所以在GirdView的DataKeyNames属性中将值改为了ID,同时代码如下:public partial class xwlb : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
void Bind()
{
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=0;database=用户");
string SQL = "select Title,Postdate from News";
SqlCommand cmd = new SqlCommand(SQL,conn);
conn.Open();
GV1.DataSource = cmd.ExecuteReader();
GV1.DataBind();
conn.Close();
} protected void GV1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int bh = Convert.ToInt32(GV1.DataKeys[e.RowIndex].Value);
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=0;database=用户");
string SQL = "delete News where ID="+bh+"";
SqlCommand cmd = new SqlCommand(SQL,conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close(); Bind();
}
}但当运行程序时,系统报错:DataBinding:“System.Data.Common.DbDataRecord”不包含名为“ID”的属性。
因为是新手,对这个不太熟悉,不知道应该如何解决,请高手们详细说明,万分感谢!
{
protected void Page_Load(object sender, EventArgs e)
{
Bind();
}
void Bind()
{
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=0;database=用户");
string SQL = "select Title,Postdate from News";
SqlCommand cmd = new SqlCommand(SQL,conn);
conn.Open();
GV1.DataSource = cmd.ExecuteReader();
GV1.DataBind();
conn.Close();
} protected void GV1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int bh = Convert.ToInt32(GV1.DataKeys[e.RowIndex].Value);
SqlConnection conn = new SqlConnection("server=(local);uid=sa;pwd=0;database=用户");
string SQL = "delete News where ID="+bh+"";
SqlCommand cmd = new SqlCommand(SQL,conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close(); Bind();
}
}但当运行程序时,系统报错:DataBinding:“System.Data.Common.DbDataRecord”不包含名为“ID”的属性。
因为是新手,对这个不太熟悉,不知道应该如何解决,请高手们详细说明,万分感谢!
public_sql.sqlshanchu("delete from T_BasicInformation_Customer where FCustomerNumbers = " + sshanchu);
string SQL = "select Title,Postdate from News";
SqlCommand cmd = new SqlCommand(SQL,conn);
conn.Open();
dataTable dt=new dataTable();
sqldataAdpter sdr=new sqldataAdpter(cmd,conn);
sdr.Fill(dataTable);
GV1.DataSource = dt; GV1.DataBind();
conn.Close();
让datatable做数据源 然后那一列为主键
另外 这个表有无ID这列,bh是否为空?
string SQL = "select Id,Title,Postdate from News";
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback)
{
Bind();
}
}先改成这样试试!