以下は、10から列データを選択しようとしたコードですが、コードは正常に機能していません
string strSuppId = GridView1.SelectedRow.Cells[10].Text;
エラー メッセージ: オブジェクト参照がオブジェクトのインスタンスに設定されていません。
私のマークアップ
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="pProductId" DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="pProductId" HeaderText="pProductId"
InsertVisible="False" ReadOnly="True" SortExpression="pProductId" />
<asp:BoundField DataField="pProductName" HeaderText="pProductName"
SortExpression="pProductName" />
<asp:BoundField DataField="pProductImage" HeaderText="pProductImage"
SortExpression="pProductImage" />
<asp:BoundField DataField="pProductType" HeaderText="pProductType"
SortExpression="pProductType" />
<asp:BoundField DataField="pProductWeight" HeaderText="pProductWeight"
SortExpression="pProductWeight" />
<asp:BoundField DataField="pProductDesign" HeaderText="pProductDesign"
SortExpression="pProductDesign" />
<asp:BoundField DataField="PProductColor" HeaderText="PProductColor"
SortExpression="PProductColor" />
<asp:BoundField DataField="pProductTransparency"
HeaderText="pProductTransparency" SortExpression="pProductTransparency" />
<asp:BoundField DataField="pProductQuantity" HeaderText="pProductQuantity"
SortExpression="pProductQuantity" />
<asp:BoundField DataField="pProductUnitPrice" HeaderText="pProductUnitPrice"
SortExpression="pProductUnitPrice" />
<asp:BoundField DataField="pSupplierId" HeaderText="pSupplierId"
SortExpression="pSupplierId" />
</Columns>
私のコードファイル
protected void btnAddPdt_Click(object sender, EventArgs e)
{
if (txtPdtName.Text == "" || ddlPdtType.SelectedItem.Text == "" || txtPrice.Text == "")
{
MessageBox.Show("Fields cannot be empty", "Error Message");
}
else
{
OleDbConnection mDB = new OleDbConnection();
mDB.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data source="
+ Server.MapPath("~/App_Data/Database.accdb");
mDB.Open();
Type csType = this.GetType();
OleDbCommand cmd;
string strSuppId = GridView1.SelectedRow.Cells[10].Text;
string sqlInsert = "INSERT INTO "
+ " Products (pProductName, pProductType, pProductWeight, pProductDesign, pProductColor, pProductTransparency, pProductQuantity, pProductUnitPrice, pSupplierId)"
+ " VALUES (@pdtName, @pdtType, @pdtWt, @pdtDesign, @pdtColor, @pdtTrans, @pdtQty, @pdtPrice, @pSuppId)";
cmd = new OleDbCommand(sqlInsert, mDB);
cmd.Parameters.AddWithValue("@pdtName", txtPdtName.Text);
cmd.Parameters.AddWithValue("@pdtType", ddlPdtType.SelectedItem.Text);
cmd.Parameters.AddWithValue("@pdtWt", txtPdtWt.Text);
cmd.Parameters.AddWithValue("@pdtDesign", txtDesign.Text);
cmd.Parameters.AddWithValue("@pdtColor", txtColor.Text);
cmd.Parameters.AddWithValue("@pdtTrans", ddlTrans.SelectedItem.Text);
cmd.Parameters.AddWithValue("@pdtQty", txtQuantity.Text);
cmd.Parameters.AddWithValue("@pdtPrice", txtPrice.Text);
cmd.Parameters.AddWithValue("@pSuppId", strSuppId);
cmd.ExecuteNonQuery();
mDB.Close();
}