Reputation: 237
In my gridview I have given a textbox for each row which has a corresponding button which fetches the value from the textbox and updates that rows value in the database.
But I want to add one button on top in the Header Template which will fetch the value of all textboxes and update their rows.
I have tried the following code but I get a null reference exception at the "instance_id cell[0]"
My code is below.
`
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField Text="Update Attendance" CommandName="Select" />
</Columns>
</asp:GridView>`
And on Button Click to fetch the data from all the textboxes:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
vo.instance_ID = GridView1.SelectedRow.Cells[0].Text;
vo.Project_ID = GridView1.SelectedRow.Cells[1].Text;
vo.volunteer_ID = GridView1.SelectedRow.Cells[6].Text;
vo.Campus_ID = GridView1.SelectedRow.Cells[5].Text;
vo.attendance_hours = (GridView1.SelectedRow.FindControl("TextBox1") as TextBox).Text;
Convert.ToInt32(vo.attendance_hours);
try
{
int success = BL.fn_UpdateAttendance(vo);
if (success == 2)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Attendance Updated Successfully')", true);
// Label2.Text = "Attendance Updated Successfully";
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Attendance Not Updated')", true);
// Label2.Text = "Attendance Not Updated";
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
Upvotes: 0
Views: 1167
Reputation: 237
My friend suggested using row.Cells[0]; inside the foreach and it worked!
Thank you everyone.
Upvotes: 0