user2832406
user2832406

Reputation: 35

Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.LinkButton'

i try to approve documents but when i click on approve button it show me error

Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.LinkButton'.

gridview

<asp:GridView ID="GrdFileApprove" runat="server" AutoGenerateColumns="False" 
                onrowcommand="GrdFileApprove_RowCommand">
               <Columns>
                   <asp:TemplateField HeaderText="S no">
                       <ItemTemplate>
                           <%# Container.DataItemIndex+1 %>
                           <asp:HiddenField runat="server" ID="HdnFileID" Value='<%# 
                      Eval("DocID") %>' />
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:BoundField DataField="DocID" HeaderText="DocumentID"  />
                   <asp:BoundField DataField="DocName" HeaderText="DocName"  />
                   <asp:BoundField DataField="Uploadfile" HeaderText="File Name" />
                   <asp:BoundField DataField="DocType" HeaderText="Document" />
                   <asp:BoundField DataField="DepType" HeaderText="Department" />
                   <asp:TemplateField HeaderText="S no">
                       <ItemTemplate>


                           <asp:Button runat="server" Id="BtnApprove"
                           CommandName="_Approve" 

                                CommandArgument='<%# Eval("DocID") %>' Text="Aprrove"
                             />

                           <asp:Button runat="server" Id="Button1" 
                         CommandName="_Reject" 
                                CommandArgument='<%# Eval("DocID") %>' Text="Reject" />
                       </ItemTemplate>
                   </asp:TemplateField>
                   <asp:ButtonField Text="Button" />
               </Columns>
           </asp:GridView>

</div>

code

if (e.CommandName == "_Approve")
        {
            //using (SqlConnection con = DataAccess.GetConnected())
            using (SqlConnection con = new 
        SqlConnection(ConfigurationManager.ConnectionStrings

         ["mydms"].ConnectionString))
            {
                try
                {
                    int rowindex = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = (GridViewRow)
          ((Control)e.CommandSource).NamingContainer;
                    LinkButton Prove_Button = 
             (LinkButton)row.FindControl("BtnApprove");
                    SqlCommand cmd = new SqlCommand("spinsertapprove", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserID", UserID));
                    cmd.Parameters.Add(new SqlParameter("@DocID", DocID));
                    cmd.Parameters.Add(new SqlParameter("@ApproveType", "Approve"));
                    int result = cmd.ExecuteNonQuery();
                    if (result != 0)
                    {
                        GrdFileApprove.DataBind();
                    }
                }

                catch
                {
                    apfi.Text = "Not Approve";



                }
                finally
                {
                    con.Close();
                }
            }
        }


        else if (e.CommandName == "_Reject")
        {
            using (SqlConnection con = new 
             SqlConnection(ConfigurationManager.ConnectionStrings

            ["mydms"].ConnectionString))
            {
                try
                {
                    int rowindex = Convert.ToInt32(e.CommandArgument);
                    GridViewRow row = (GridViewRow)
              ((Control)e.CommandSource).NamingContainer;
                    LinkButton Prove_Button = (LinkButton)row.FindControl("Button1");
                    SqlCommand cmd = new SqlCommand("sprejectapprove", con);

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("@UserID",UserID));
                    cmd.Parameters.Add(new SqlParameter("@DocID", DocID));
                    cmd.Parameters.Add(new SqlParameter("@ApproveType", "Reject"));
                    int result = cmd.ExecuteNonQuery();
                    if (result != 0)
                    {
                        GrdFileApprove.DataBind();
                    }
                }

                catch 
                {
                    apfi.Text = "Rejct";
                }
                finally
                {
                    con.Close();
                }
            }
        }

it shows me error in catch when click on approve button

Upvotes: 1

Views: 21235

Answers (3)

boateng
boateng

Reputation: 898

var btnApprove = (Button)e.CommandSource;

Upvotes: 0

Chris
Chris

Reputation: 4671

First of all, the error message is telling you exactly what's wrong - it can't cast it to a LinkButton because it isn't a LinkButton, you've used a normal Button.

Second of all, I don't see why you're bothering to get hold of the button at all anyway, let alone cast it, because you don't seem to be doing anything with it at all. Once you've stored it in your Prove_Button variable, you never touch Prove_Button again to do anything with it, so there's no point having that in the first place.

Upvotes: 0

Kippie
Kippie

Reputation: 3820

Problem and sollution should be pretty straightforward.

In your code you cast your button to a LinkButton:

(LinkButton)row.FindControl("BtnApprove");

When it's clear that inside your html, you use a regular button:

<asp:Button runat="server" Id="BtnApprove"
                           CommandName="_Approve" 

                                CommandArgument='<%# Eval("DocID") %>' Text="Aprrove"
                             />

Just change it so you cast to Button instead and all should be fine

(Button)row.FindControl("BtnApprove");

Upvotes: 2

Related Questions