我有GridView控件中包含的LinkButton如下面我aspx文件鳕鱼:GridView的选择行返回null
<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>
我要当链接按钮用户点击更新我的数据库表,但是当我想要得到的GridView细胞的价值选定的行我面临空行参考异常行: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();
}
因此,这行抛出异常,和你做了调查,为什么? –
in this line: string barcode = dgvData.SelectedRow.Cells [12] .Text; –
no不是这种情况,我gridview的选定行属性返回null。 –