我有以下代碼:對象引用不設置爲與字符串數組的對象信息的實例
protected void btnSave_Click(object sender, EventArgs e)
{
StarBusModel.BookingDetail1 objBooking = new StarBusModel.BookingDetail1();
objBooking.TourID = tourID;
objBooking.Name = txtName.Text;
objBooking.Phone = txtPhone.Text;
objBooking.Amount = Convert.ToDecimal(Request.Form[txtAmount.ClientID]);
string [] seats = Request.Form[txtSeatNo.ClientID].Split(new char[] {','});
for (int i = 0; i < seats.Length; i++)
objBooking.SeatDetails.Add(new StarBusModel.SeatDetail1() {SeatID = Convert.ToInt32(seats[i])});
objEntities.BookingDetail1.AddObject(objBooking);
objEntities.SaveChanges();
BindSeats();
}
我得到錯誤
System.NullReferenceException: Object reference not set to an instance of an object.
在以下代碼:
string [] seats = Request.Form[txtSeatNo.ClientID].Split(new char[] {','});
請告知我錯在哪裏。我搜索了各種答案,但無法確定我錯在哪裏。
此'Request.Form [txtSeatNo.ClientID]'可能會給你'null'? –
在進行任何操作之前,對輸入表單值執行完整性檢查總是很好的做法。順便說一下,爲什麼你需要拆分txtSeatNo? – Turbot
爲什麼你不能訪問txtAmount.Text和txtSeatNo.Text?爲什麼你需要使用表單集合獲得控制值? –