我使用带有c#的asp.net进行gridview。 它显示了sql server中表的数据。当我更新某一行时,我得到了这条消息: 对象引用未设置为对象的实例 我尝试了很多代码,但没有工作.. 你能帮我解决这个问题吗?感谢。
这是gridview代码
<asp:GridView ID="gvIqamaAlert" runat="server" AutoGenerateColumns="False"
DataKeyNames="EmpNo" onrowcancelingedit="gvIqamaAlert_RowCancelingEdit"
onrowediting="gvIqamaAlert_RowEditing" onrowupdating="gvIqamaAlert_RowUpdating"
Width="828px">
<Columns>
<asp:BoundField DataField="Nationality" HeaderText="الجنسيه" ReadOnly="True"
SortExpression="Nationality" />
<asp:TemplateField ControlStyle-BorderStyle="NotSet"
HeaderText="تاريخ الميلاد الهجري">
<EditItemTemplate>
<asp:Label ID="Label21" runat="server"
Text='<%# GetHijri(String.Format("{0:dd-MM-yyyy}", Eval("DOB"))) %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server"
Text='<%# GetHijri(String.Format("{0:dd-MM-yyyy}", Eval("DOB"))) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="تاريخ الميلاد" SortExpression="DOB">
<EditItemTemplate>
<asp:Label ID="Label11" runat="server"
Text='<%# String.Format("{0:dd-MM-yyyy}", Eval("DOB")) %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# String.Format("{0:dd-MM-yyyy}", Eval("DOB")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="جهة الاصدار" SortExpression="IDIssPlace">
<EditItemTemplate>
<asp:TextBox ID="txtIssP" runat="server" Text='<%# Eval("IDIssPlace") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("IDIssPlace") %>'></asp:Label>
</ItemTemplate>
<asp:TemplateField HeaderText="الاسم" SortExpression="FName">
<EditItemTemplate>
<asp:TextBox ID="txtfname" runat="server" Text='<%# Eval("FName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("FName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="الرقم الوظيفي">
<EditItemTemplate>
<asp:Label ID="Labe1" runat="server" Text='<%# Eval("EmpNo") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("EmpNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lbtnUpdate" RunAt="server" CommandName="Update"
Text="موافق" />
<asp:LinkButton ID="lbtnCancel" RunAt="server" CommandName="Cancel"
Text="الغاء" />
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit" RunAt="server" CommandName="Edit" Text="تعديل" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码背后:
private SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=dbHrSys;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindEmployeeDetails();
}
}
protected void BindEmployeeDetails()
{
con.Open();
SqlCommand cmd = new SqlCommand(" Select IDNo, EmpNo, Nationality, IDIssDate, IDExpDate, IDIssPlace, FName, DOB, Flag FROM Employee WHERE (IDExpDate <= DATEADD(day, 30, GETDATE())) AND (Flag = 2)", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
gvIqamaAlert.DataSource = ds;
gvIqamaAlert.DataBind();
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
gvIqamaAlert.DataSource = ds;
gvIqamaAlert.DataBind();
int columncount = gvIqamaAlert.Rows[0].Cells.Count;
gvIqamaAlert.Rows[0].Cells.Clear();
gvIqamaAlert.Rows[0].Cells.Add(new TableCell());
gvIqamaAlert.Rows[0].Cells[0].ColumnSpan = columncount;
gvIqamaAlert.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void gvIqamaAlert_RowEditing(object sender, GridViewEditEventArgs e)
{
gvIqamaAlert.EditIndex = e.NewEditIndex;
BindEmployeeDetails();
}
protected void gvIqamaAlert_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label l = (Label)gvIqamaAlert.Rows[e.RowIndex].FindControl("Label1");
string jobId = gvIqamaAlert.DataKeys[e.RowIndex].Value.ToString();
TextBox fname = (TextBox)gvIqamaAlert.FindControl("txtfname");
TextBox IssP = (TextBox)gvIqamaAlert.FindControl("txtIssP");
////TextBox Issd = (TextBox)gvIqamaAlert.FindControl("txtIssd");
////temp1 = ((TextBox)gvIqamaAlert.FindControl("txtIssd")).Text;
//DateTime Iss = DateTime.ParseExact(Greg(((TextBox)gvIqamaAlert.FindControl("txtIssd")).Text), "dd/MM/yyyy", null);
//TextBox Expd = (TextBox)gvIqamaAlert.FindControl("txtExpd");
//temp2 = Greg(Expd.ToString());
//DateTime Exp = DateTime.ParseExact(temp2, "dd/MM/yyyy", null);
con.Open();
SqlCommand cmd = new SqlCommand("Update Employee set FName='" + fname.Text + "',IDIssPlace='" + IssP.Text + "' where EmpNo='" + Convert.ToString(l.Text) + "'", con);
cmd.Parameters.AddWithValue("@id", jobId);
//,IDIssDate = @iss, IDExpDate = @exp
//cmd.Parameters.AddWithValue("@iss",Iss);
//cmd.Parameters.AddWithValue("@exp", Exp);
cmd.ExecuteNonQuery();
con.Close();
gvIqamaAlert.EditIndex = -1;
BindEmployeeDetails();
}
protected void gvIqamaAlert_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvIqamaAlert.EditIndex = -1;
BindEmployeeDetails();
}
答案 0 :(得分:1)
由于此代码
,我得到了nullTextBox fname = (TextBox)gvIqamaAlert.FindControl("txtfname");
TextBox IssP = (TextBox)gvIqamaAlert.FindControl("txtIssP");
.Rows [e.RowIndex]丢失了。新代码:
TextBox fname = (TextBox)gvIqamaAlert.Rows[e.RowIndex].FindControl("txtfname");
TextBox IssP = (TextBox)gvIqamaAlert.Rows[e.RowIndex].FindControl("txtIssP");
现在它运作良好。谢谢你的帮助。
答案 1 :(得分:0)
尝试使用以下代码获取EmpNO:
int ro = gvr.RowIndex;
string EmpNO = gvDetails.DataKeys[ro].Value.ToString();
而不是这一个:
Label l = (Label)gvIqamaAlert.Rows[e.RowIndex].FindControl("Label1");
然后将SQL命令更改为以下内容:
SqlCommand cmd = new SqlCommand("Update Employee set FName='" + fname.Text + "',IDIssPlace='" + IssP.Text + "' where EmpNo='" + EmpNO + "'", con);
答案 2 :(得分:0)
请使用提及代码重复以下代码:
protected void gvIqamaAlert_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label l = (Label)gvIqamaAlert.Rows[e.RowIndex].FindControl("Label1");
string jobId = gvIqamaAlert.DataKeys[e.RowIndex].Value.ToString();
TextBox fname = (TextBox)gvIqamaAlert.FindControl("txtfname");
TextBox IssP = (TextBox)gvIqamaAlert.FindControl("txtIssP");
if(l == null || fname == null || IssP == null) return;
////TextBox Issd = (TextBox)gvIqamaAlert.FindControl("txtIssd");
////temp1 = ((TextBox)gvIqamaAlert.FindControl("txtIssd")).Text;
//DateTime Iss = DateTime.ParseExact(Greg(((TextBox)gvIqamaAlert.FindControl("txtIssd")).Text), "dd/MM/yyyy", null);
//TextBox Expd = (TextBox)gvIqamaAlert.FindControl("txtExpd");
//temp2 = Greg(Expd.ToString());
//DateTime Exp = DateTime.ParseExact(temp2, "dd/MM/yyyy", null);
con.Open();
SqlCommand cmd = new SqlCommand("Update Employee set FName='" + fname.Text + "',IDIssPlace='" + IssP.Text + "' where EmpNo='" + Convert.ToString(l.Text) + "'", con);
cmd.Parameters.AddWithValue("@id", jobId);
//,IDIssDate = @iss, IDExpDate = @exp
//cmd.Parameters.AddWithValue("@iss",Iss);
//cmd.Parameters.AddWithValue("@exp", Exp);
cmd.ExecuteNonQuery();
con.Close();
gvIqamaAlert.EditIndex = -1;
BindEmployeeDetails();
}
答案 3 :(得分:0)
问题是你在尝试在GridView中找到控件而不传递Row索引
TextBox fname = (TextBox)gvIqamaAlert.FindControl("txtfname");
TextBox IssP = (TextBox)gvIqamaAlert.FindControl("txtIssP");
尝试按照您在Label1
中所做的那样传递行索引 Label l = (Label)gvIqamaAlert.Rows[e.RowIndex].FindControl("Label1");
是这样的:
TextBox fname = (TextBox)gvIqamaAlert.Rows[e.RowIndex].FindControl("txtfname");
TextBox IssP = (TextBox)gvIqamaAlert.Rows[e.RowIndex].FindControl("txtIssP");
答案 4 :(得分:0)
行编辑:当用户点击编辑按钮时,将执行网格视图的行编辑事件。此事件将提供行索引
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
databind();
}
行更新:这将执行回发,将执行网格视图的行更新事件过程
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label l = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");
TextBox t1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
TextBox t2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
FileUpload fu = (FileUpload)GridView1.Rows[e.RowIndex].FindControl("FileUpload1");
string fpath = Server.MapPath("images");
string fname = fu.FileName;
string concat = fpath + "\\" + fname;
fu.SaveAs(concat);
cmd = new SqlCommand("update userdata set username='" + t1.Text + "', password='" + t2.Text + "' , Image = '" + "~/images/"+ fu.FileName + "' where userid='" + Convert.ToInt32(l.Text) + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
databind();
}