0

Below is the code from where i tried to select the column data from the 10, however code is not working as it should be

  string strSuppId = GridView1.SelectedRow.Cells[10].Text;

ERROR MSG:Object reference not set to an instance of an object.

my markup

<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>

my code file

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();
    }
  • 1
    "Not working as it should be" doesn't exactly help us understand what is going on. What isn't working? What happens when it doesn't work? What SHOULD happen when it works? – freefaller Jan 22 '13 at 14:02
  • Is the 11th column a `BoundField` or a `TemplateField`? (note that collections are zero based in .NET) – Tim Schmelter Jan 22 '13 at 14:03
  • @MalcolmNathanielNg: Show the aspx markup of the grid. It's also important to know what _"not working"_ actually means(**never**, without excuses, use this term to describe your problem on SO!). Where do you want to get the value(f.e. in an event handler, page_load, etc.)? – Tim Schmelter Jan 22 '13 at 15:30
  • Share the code block you written... then it was easy to help... – Pandian Jan 22 '13 at 15:34
  • hi guys just edited it the code file is trigger by a button – Malcolm Nathaniel Ng Jan 22 '13 at 15:35
  • actually what you are trying to achieve...? bulk insert of grid view to DB...? – Pandian Jan 22 '13 at 15:42
  • No @pan what I am trying to do is to read pSupplierId in the gridview and pass it to a variable – Malcolm Nathaniel Ng Jan 23 '13 at 07:51

1 Answers1

0

Try the below coding...

Declare a variable as Global

public string _Data = "";

Write the Gridview SelectedIndexed event like below

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
    string data = "";
    GridViewRow row = GridView1.SelectedRow;
    _Data = row.Cells[10].Text;
}

then change your Coding like below

string strSuppId = GridView1.SelectedRow.Cells[10].Text;

to

string strSuppId = _Data

Before the Button_Click Select the Row from GridView... then only it returns Data....

Pandian
  • 8,848
  • 2
  • 23
  • 33