locked
Extract column value from front end on click of Select button RRS feed

  • Question

  • User-1821620314 posted

    I have a Gridview that is used to show some limited data from a single table. On click of the Select button, I want to extract the value of the ID column which is displayed on the web page.

    For Example, Gridview values are as follows

    ID

    11

    12

    13

    In the above example whenever I click on the Select button near 11 I want to extract 11. I have tried all the possible codes but it is not working. If I write a RowCommand method then it extracts 0, not 11. Here I don't want Row Index I want data that is displayed. I kindly request to provide me perfect solution for this problem ASAP.

    Sunday, September 6, 2020 10:22 AM

All replies

  • User-943250815 posted

    Place ID in DatakeyNames property of Gridview
    Here is a sample https://www.aspsnippets.com/Articles/DataKeyNames-in-GridView-example-in-ASPNet.aspx

    Sunday, September 6, 2020 11:56 AM
  • User1535942433 posted

    Hi mohitjoshi72,

    Accroding to your descrption,as far as I think,you could use button click event.And then you could get the row which you have selected.You could find the control or column to get the value.

    Since you don't post your codes,I have created a demo.

    More details,you could refer to below codes:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="Id">
                            <ItemTemplate>
                                <asp:label runat="server" ID="lbId" Text='<%# Eval("Id")%>'></asp:label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    Code-Behind:

    protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from Test9";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "Test9");
                this.GridView1.DataSource = ds.Tables[0].DefaultView;
                this.GridView1.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                GridViewRow gvr = (GridViewRow)btn.NamingContainer;
                Label id = (Label)gvr.FindControl("lbId");
                Response.Write("<script>alert('"+ id.Text+"');</script>");
    
            }

    Result:

    Best regards,

    Yijing Sun

    Monday, September 7, 2020 2:04 AM
  • User-1821620314 posted

    @jzero

    I have tried the code given in the link but I am getting Exception as "Object Reference not set to an object type".

    Monday, September 7, 2020 1:47 PM
  • User-1821620314 posted

    @yij sun

    The code is giving some validation error on the web page

    Monday, September 7, 2020 1:50 PM
  • User1535942433 posted

    Hi mohitjoshi72,

    Could you tell us the details of the validation error or post your current full codes to us?

    It will help us to solve your problems.

    Best regards,

    Yijing Sun

    Tuesday, September 8, 2020 2:47 AM
  • User-1821620314 posted

    Exception Details

    The code of Gridview is as follows

    <asp:GridView ID="FamilyData" runat="server" OnRowCommand="FamilyData_RowCommand" AutoGenerateColumns="False" AutoGenerateSelectButton="True" DataKeyNames="Mid">
            <Columns>
                <asp:BoundField DataField="Mid" HeaderText="ओळख क्र." SortExpression="Mid" />
                <asp:BoundField DataField="Mname" HeaderText="परिवारातील व्यक्तीचे नाव" SortExpression="Mname" />
                <asp:BoundField DataField="MDOB" HeaderText="जन्म तारीख" SortExpression="MDOB" />
                <asp:BoundField DataField="Mcontact" HeaderText="संपर्क क्र." SortExpression="Mcontact" />
            </Columns>
        </asp:GridView>

    The code of the RowCommand method is as follows

    protected void FamilyData_RowCommand(object sender, GridViewCommandEventArgs e)
    {
          int rowIndex = ((sender as Button).NamingContainer as GridViewRow).RowIndex;
          int index = Convert.ToInt32(FamilyData.DataKeys[rowIndex].Values[0]);
          Response.Redirect("~/UpdateMember.aspx?Mid=" + index);            
    }

    After running the code I m getting the exception 'Object reference not set to an instance of an object.' at the first line of the RowCommand method.

    Tuesday, September 15, 2020 12:48 PM
  • User-943250815 posted

    There is no CommandField or Template Field with button on your Gridview to fire RowCommand
    Check again link posted by me and sample from Yij Sun, in both, button is in Template Field.

    Tuesday, September 15, 2020 4:54 PM
  • User1535942433 posted

    Hi mohitjoshi72,

    Accroding to your description and codes,you have used AutoGenerateSelectButton,however,you cann't find the button.So,I suggest you could use SelectedIndexChanged event to get the rowindex.As far as I think,this is the more simple solution to your codes.

    Just like this:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AutoGenerateSelectButton="True" DataKeyNames="ID" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                    <Columns>                 
                        <asp:BoundField DataField="Id" HeaderText="ID" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

    Code-Behind:

      protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from Test9";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "Test9");
                this.GridView1.DataSource = ds.Tables[0].DefaultView;
                this.GridView1.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Button btn = (Button)sender;
                GridViewRow gvr = (GridViewRow)btn.NamingContainer;
                string id = gvr.Cells[0].Text;
                Response.Write("<script>alert('" + id + "');</script>");
    
            }
            protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                GridViewRow row = GridView1.SelectedRow;
                int index = Convert.ToInt32(row.RowIndex);
                Response.Write("<script>alert('" + index + "');</script>");
            }

    Best regards,

    Yijing Sun

    Wednesday, September 16, 2020 5:34 AM
  • User-1821620314 posted

    I have tried code given on this link but I m getting Exception as 'Object reference not set to an instance of an object.'

    Wednesday, February 3, 2021 9:37 AM