我在創建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