Reputation: 47
i have gridview contains linkbutton as below cod in my aspx file:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Button3" Style="float: left; margin-left: 10px"
CommandArgument='<%# Bind("ID") %>'
Text="cancellation" runat="server" CommandName="cancell" OnClick="Button3_Click">
<i aria-hidden="true" class="icon-lock" title="cancellation"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
I want when user click on link button update my database table but when I want get value of cell from gridview selected row I face by null refrence exception in line: string barcode = dgvData.SelectedRow.Cells[12].Text;.
protected void Button3_Click(object sender, EventArgs e)
{
Transaction tr = new Transaction();
HasinReservation.Entities.Db.Transaction dt = new Transaction();
SqlConnection connection = new SqlConnection(@"Data Source=192.x.x.x\Sql2008;Initial Catalog=GardeshgariKish;User ID=cms;Password=******;MultipleActiveResultSets=True;Application Name=EntityFramework");
connection.Open();
SqlCommand sqlCmd = new SqlCommand("Update Transactions SET IsCancelled = 1 WHERE BarCodeNumber = @Value", connection);
string barcode = dgvData.SelectedRow.Cells[12].Text;
sqlCmd.Parameters.AddWithValue("@Value", barcode);
//sqlCmd.Parameters.AddWithValue("@Value2", DropDownList2.SelectedItem.Text);
sqlCmd.ExecuteNonQuery();
connection.Close();
}
Upvotes: 1
Views: 7439
Reputation: 47
thank you for your concern i found it. here is my edited asp markup:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Button3" Style="float: left; margin-left: 10px"
CommandArgument='<%# Bind("ID") %>'
Text="cancellation" runat="server" **CommandName="select"** OnClick="Button3_Click">
<i aria-hidden="true" class="icon-lock" title="cancellation"></i>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and here is code behind:
protected void Button3_Click(object sender, EventArgs e)
{
Transaction tr = new Transaction();
**GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;**
HasinReservation.Entities.Db.Transaction dt = new Transaction();
**int x = clickedRow.RowIndex;**
SqlConnection connection = new SqlConnection(@"Data Source=192.X.X.X\Sql2008;Initial Catalog=GardeshgariKish;User ID=cms;Password=XXXXXX;MultipleActiveResultSets=True;Application Name=EntityFramework");
connection.Open();
SqlCommand sqlCmd = new SqlCommand("Update Transactions SET IsCancelled = 1 WHERE BarCodeNumber = @Value", connection);
**string barcode = dgvData.Rows[x].Cells[12].Text;**
sqlCmd.Parameters.AddWithValue("@Value", barcode);
sqlCmd.ExecuteNonQuery();
connection.Close();
}
Upvotes: 1
Reputation: 45967
that's because you are not using the select-command
the way to go should be:
Add the RowCommand Event to your GridView
onrowcommand="GridView1_RowCommand"
replace the Command Argument id
with BarCode and remove OnClick="Button3_Click"
<asp:LinkButton ID="Button3" CommandArgument='<%# Bind("BarCode")
receive the value
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cancell")
{
string barcode = e.CommandArgument;
}
}
Upvotes: 2
Reputation: 10565
GridView.SelectedRow
property will be populated with the row only when you select a row. Easiest way is to set the Property autogenerateselectbutton
to true
as:
<asp:gridview id="CustomersGridView" autogenerateselectbutton="True" .../>
You must perform the below steps sequentially:
Also, dgvData.SelectedRow.Cells[12].Text
means the Cell/Column 12 should be a BoundField
.
If Column 12 is Template Field, use the FindControl()
method as dgvData.SelectedRow.FindControl("lblBarCode") as Label).Text;
assuming a Label is used to display bar code etc...
Upvotes: 2
Reputation: 696
You can try this way to get the value from GridView. I'm not writing the entire code of yours but the error containing part.
protected void Button3_Click(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.Rows[e.RowIndex];
string barcode = row.Cells["ID"].Value.ToString();
}
}
But what I can see from Your code that You're using edit mode of GridView but not leveraging it into Your code. Please see this link to To Edit And Update Rows In GridView In Asp.Net.
Upvotes: 2
Reputation: 21815
dgvData.SelectedRow
won't give you the row from which the button was clicked. You need NamingContainer
for that. This should work for you:-
LinkButton Button3 = (LinkButton)sender;
GridViewRow selectedRow = (GridViewRow)Button3.NamingContainer;
string barcode = selectedRow.Cells[12].Text;
Upvotes: 2