user2605927
user2605927

Reputation: 53

add columns to the right in gridview

I have a grid view and when i click on the radiobutton approve it should update the recommend tag value to 1 based on the employeeID.However I am getting an error saying that object is not set to an instance of an object..My columns are also being added to the left..instead of right..Below is the code I have tried.

C# code

public void GridViewBind()
        {
            dadapter = new SqlDataAdapter("SELECT M_Emp_Personal.EmpName, M_Division.DivShort, M_Designation.DesigShort, T_TADA_tempform.BasicSalary, T_TADA_tempform.GPFNo, T_TADA_tempform.Gradepay,T_TADA_tempform.move_date, T_TADA_tempform.purpose, M_City.CityDesc, T_TADA_tempform.estt_visited, T_TADA_tempform.duration_stay, M_mode.mode_type, T_TADA_tempform.duration_unit, T_TADA_tempform.place, T_TADA_tempform.authority, T_TADA_tempform.exp_debited, T_TADA_tempform.reason FROM T_TADA_tempform INNER JOIN M_Emp_Personal ON T_TADA_tempform.EmpID = M_Emp_Personal.EmpID INNER JOIN M_Division ON T_TADA_tempform.DivisionID = M_Division.DivisionID INNER JOIN M_Designation ON M_Emp_Personal.DesigID = M_Designation.DesigID INNER JOIN M_City ON T_TADA_tempform.CityID = M_City.CityID INNER JOIN M_mode ON T_TADA_tempform.mode_ID = M_mode.mode_ID where M_Emp_Personal.EmpID=" + ddlname.SelectedValue + "", conn);
            dset = new DataSet();
            dadapter.Fill(dset);
            GridView1.DataSource = dset.Tables[0];
            GridView1.DataBind();
        }


protected void submit_info(object sender, EventArgs e)
        {


            GridViewRow grow = (GridViewRow)(sender as Control).Parent.Parent;

            RadioButton rbpApprove = (RadioButton)grow.FindControl("rbtnapprove");
            RadioButton rbpReject = (RadioButton)grow.FindControl("rbtnreject");



            if (rbpApprove.Checked == true)
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("UPDATE T_TADA_tempform SET Recommened_tag =1 where EmpID=@EmpID", conn);

                cmd.Parameters.AddWithValue("@EmpID", ddlname.SelectedValue);

                conn.Close();

            }

this is my ASP.NET code

 <asp:GridView ID="GridView1" runat="server"  CssClass="vutblrow" TabIndex="6" 
 CellPadding="4" ForeColor="#333333" GridLines="None"  Width="100%"  
PagerStyle-Mode="NumericPages" > 
<PagerStyle CssClass="pgr"  Height="25px" BorderStyle="Solid" />

<Columns>
<asp:TemplateField HeaderText="Approve">
<ItemTemplate>
<asp:RadioButton  runat="server" GroupName="status"  />
                                                                   </ItemTemplate>                                                                                                                                  
</asp:TemplateField>

<asp:TemplateField HeaderText="Reject">
<ItemTemplate>
<asp:RadioButton   runat="server" GroupName="status"  />
                                                                   </ItemTemplate>                                                                                                                                  
</asp:TemplateField><asp:TemplateField HeaderText="Submit">

<ItemTemplate>
<asp:Button CssClass="btnAction" Text="Sumbit"  runat="server" OnClick="submit_info"  />
                                                                  </ItemTemplate>                                                                                                                              
</asp:TemplateField>

                                                                </Columns>                                                        
<HeaderStyle CssClass="vutblhdr" />

</asp:GridView>

Upvotes: 0

Views: 81

Answers (2)

Hans Derks
Hans Derks

Reputation: 1027

I think you are missing the Ids of the radionbuttons, try this

        <asp:TemplateField HeaderText="Approve">
            <ItemTemplate>
                <asp:RadioButton ID="rbtnapprove" runat="server" GroupName="status" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Reject">
            <ItemTemplate>
                <asp:RadioButton ID="rbtnreject" runat="server" GroupName="status" />
            </ItemTemplate>
        </asp:TemplateField>

And call Execute

if (rbpApprove.Checked == true)
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("UPDATE T_TADA_tempform SET Recommened_tag =1 where EmpID=@EmpID", conn);

                cmd.Parameters.AddWithValue("@EmpID", ddlname.SelectedValue);
                cmd.ExecuteNonQuery();

                conn.Close();

            }

Upvotes: 0

Chris L
Chris L

Reputation: 2292

You will see that error when you're trying to use an object that hasn't been initialised correctly.

Likely candidates are:

 RadioButton rbpApprove = (RadioButton)grow.FindControl("rbtnapprove");
 RadioButton rbpReject = (RadioButton)grow.FindControl("rbtnreject");

If the rbpApprove Control isn't found then this line here:

if (rbpApprove.Checked == true)

Will give you the error you are getting.

However only seeing a subset of your code it could be elsewhere.

Upvotes: 1

Related Questions