我在创建datarow对象的位置出现错误。尝试在数据集表中插入新行时出错
对象引用不设置到对象的实例
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con2 = new SqlConnection("Data Source=AMIT-PC\\SQLEXPRESS;Initial Catalog=Amit;Integrated Security=true");
con2.Open();
SqlCommand cmd2 = new SqlCommand("Select * from hotel.country", con2);
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
SqlCommandBuilder sb = new SqlCommandBuilder(da2);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
/* GridView2.DataSource = ds2;
GridView2.DataBind();*/
DataRow dr = ds2.Tables["hotel.country"].NewRow();
dr[ds2.Tables[TextBox1.Text].Columns[0].ColumnName] = TextBox2.Text;
dr[ds2.Tables[TextBox1.Text].Columns[1].ColumnName] = TextBox3.Text;
da2.Update(ds2);
GridView1.DataBind();
}
我不知道为什么,谁只是评论的家伙删除他的帖子,因为它解决了我的问题......那是我不能在ds2.Tables [“”]中使用表名,而应该使用数字。 – 2013-03-09 10:52:48
删除它,因为我有关于存储过程的一些信息,并且您正在使用select命令。我现在改变了我的答案。 – JodyT 2013-03-09 10:56:51
如果你将有超过5张桌子,你会记得所有的数字吗?尝试'DataRow dr = ds2.Tables [“country”]。NewRow()' 如果您使用table Country等创建了像DsHotel一样的自己的数据集,那么它会容易得多。那么它将是 'DsHotel.CountryRow dr = DsHotel.Country.NewRow(); dr.YourColumnName = ....' – 2013-03-09 11:05:33