Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.TextBox'

Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.TextBox'

Scheduled Pinned Locked Moved ASP.NET
csharpdesignsysadminhelpannouncement
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haleemasher
    wrote on last edited by
    #1

    When i run the code below it show error in bold line of code that "Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.TextBox'" as i m new to C# programming so i can't resolve it.

    protected void gvCouponDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    int id;
    int CouponTypeID;
    int Serial_Start;
    int serial_End;
    TextBox CouponName;
    DateTime TranDate;

    GridViewRow row = gvCouponDetails.Rows\[e.RowIndex\];
    
    id = int.Parse(((Label)row.Cells\[0\].FindControl("lblID")).Text);
    TranDate = DateTime.Parse(((TextBox)row.Cells\[2\].FindControl("tbDate")).Text);
    

    ** CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();**
    Serial_Start= int.Parse(((TextBox)row.Cells[4].FindControl("tbSerialStart")).Text);
    serial_End = int.Parse(((TextBox)row.Cells[5].FindControl("tbSerialEnd")).Text);

    string UpdateQuery = string.Format("update Coupon\_Info Set ReceivingDate = '{0}',Coupon\_TypeID = {1}, Serial\_Start={2},Serial\_End={3} where ID= {4}",TranDate.ToString(),CouponTypeID.ToString(),Serial\_Start.ToString(),serial\_End.ToString(),id.ToString());
    DAL da = new DAL();
    da.ConnectionString = ConfigurationManager.AppSettings\["ConnectionString"\].ToString();
    int i= da.ExecuteNonQuery(CommandType.Text, UpdateQuery, null);
    gvCouponDetails.EditIndex = -1;
    boundgridview();
    

    }

    ASP code is

    <asp:GridView ID="gvCouponDetails"
    runat="server"
    AutoGenerateColumns="false"
    onrowcancelingedit="gvCouponDetails_RowCancelingEdit"
    onrowediting="gvCouponDetails_RowEditing"
    onrowupdating="gvCouponDetails_RowUpdating"
    DataKeyNames="ID"
    style="top: 521px; left: 276px; position: absolute; height: 143px; width: 704px">
    <Columns>

            <asp:CommandField   ButtonType="Button"
                                CancelText="C"
                                ShowCancelButton="true"
                                EditText="E"
                                ShowEditButton="true"
                                ControlStyle-Width="15px"
                                ControlStyle-Font-Size="10px"
                                ControlStyle-ForeColor="Blue"
                                UpdateText="U"  />
    
                
    
    
                
                <asp:TemplateField HeaderText="Coupon Name">
    
    A A C P H 5 Replies Last reply
    0
    • H haleemasher

      When i run the code below it show error in bold line of code that "Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.TextBox'" as i m new to C# programming so i can't resolve it.

      protected void gvCouponDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
      int id;
      int CouponTypeID;
      int Serial_Start;
      int serial_End;
      TextBox CouponName;
      DateTime TranDate;

      GridViewRow row = gvCouponDetails.Rows\[e.RowIndex\];
      
      id = int.Parse(((Label)row.Cells\[0\].FindControl("lblID")).Text);
      TranDate = DateTime.Parse(((TextBox)row.Cells\[2\].FindControl("tbDate")).Text);
      

      ** CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();**
      Serial_Start= int.Parse(((TextBox)row.Cells[4].FindControl("tbSerialStart")).Text);
      serial_End = int.Parse(((TextBox)row.Cells[5].FindControl("tbSerialEnd")).Text);

      string UpdateQuery = string.Format("update Coupon\_Info Set ReceivingDate = '{0}',Coupon\_TypeID = {1}, Serial\_Start={2},Serial\_End={3} where ID= {4}",TranDate.ToString(),CouponTypeID.ToString(),Serial\_Start.ToString(),serial\_End.ToString(),id.ToString());
      DAL da = new DAL();
      da.ConnectionString = ConfigurationManager.AppSettings\["ConnectionString"\].ToString();
      int i= da.ExecuteNonQuery(CommandType.Text, UpdateQuery, null);
      gvCouponDetails.EditIndex = -1;
      boundgridview();
      

      }

      ASP code is

      <asp:GridView ID="gvCouponDetails"
      runat="server"
      AutoGenerateColumns="false"
      onrowcancelingedit="gvCouponDetails_RowCancelingEdit"
      onrowediting="gvCouponDetails_RowEditing"
      onrowupdating="gvCouponDetails_RowUpdating"
      DataKeyNames="ID"
      style="top: 521px; left: 276px; position: absolute; height: 143px; width: 704px">
      <Columns>

              <asp:CommandField   ButtonType="Button"
                                  CancelText="C"
                                  ShowCancelButton="true"
                                  EditText="E"
                                  ShowEditButton="true"
                                  ControlStyle-Width="15px"
                                  ControlStyle-Font-Size="10px"
                                  ControlStyle-ForeColor="Blue"
                                  UpdateText="U"  />
      
                  
      
      
                  
                  <asp:TemplateField HeaderText="Coupon Name">
      
      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      haleemasher wrote:

      TextBox CouponName;

      Why CoupanName is TextBox Type ?

      haleemasher wrote:

      CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();

      Use

      String CouponName;
      CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();

      Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

      H S 2 Replies Last reply
      0
      • A Abhijit Jana

        haleemasher wrote:

        TextBox CouponName;

        Why CoupanName is TextBox Type ?

        haleemasher wrote:

        CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();

        Use

        String CouponName;
        CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();

        Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

        H Offline
        H Offline
        haleemasher
        wrote on last edited by
        #3

        I change my code as u suggest but now it show this error in same line of code**'System.Web.UI.Control' does not contain a definition for 'SelectedValue' and no extension method 'SelectedValue' accepting a first argument of type 'System.Web.UI.Control' could be found**

        CouponName =(DropDownList)row.Cells[3].FindControl("ddlCoupon").SelectedValue.ToString();

        1 Reply Last reply
        0
        • H haleemasher

          When i run the code below it show error in bold line of code that "Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.TextBox'" as i m new to C# programming so i can't resolve it.

          protected void gvCouponDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
          {
          int id;
          int CouponTypeID;
          int Serial_Start;
          int serial_End;
          TextBox CouponName;
          DateTime TranDate;

          GridViewRow row = gvCouponDetails.Rows\[e.RowIndex\];
          
          id = int.Parse(((Label)row.Cells\[0\].FindControl("lblID")).Text);
          TranDate = DateTime.Parse(((TextBox)row.Cells\[2\].FindControl("tbDate")).Text);
          

          ** CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();**
          Serial_Start= int.Parse(((TextBox)row.Cells[4].FindControl("tbSerialStart")).Text);
          serial_End = int.Parse(((TextBox)row.Cells[5].FindControl("tbSerialEnd")).Text);

          string UpdateQuery = string.Format("update Coupon\_Info Set ReceivingDate = '{0}',Coupon\_TypeID = {1}, Serial\_Start={2},Serial\_End={3} where ID= {4}",TranDate.ToString(),CouponTypeID.ToString(),Serial\_Start.ToString(),serial\_End.ToString(),id.ToString());
          DAL da = new DAL();
          da.ConnectionString = ConfigurationManager.AppSettings\["ConnectionString"\].ToString();
          int i= da.ExecuteNonQuery(CommandType.Text, UpdateQuery, null);
          gvCouponDetails.EditIndex = -1;
          boundgridview();
          

          }

          ASP code is

          <asp:GridView ID="gvCouponDetails"
          runat="server"
          AutoGenerateColumns="false"
          onrowcancelingedit="gvCouponDetails_RowCancelingEdit"
          onrowediting="gvCouponDetails_RowEditing"
          onrowupdating="gvCouponDetails_RowUpdating"
          DataKeyNames="ID"
          style="top: 521px; left: 276px; position: absolute; height: 143px; width: 704px">
          <Columns>

                  <asp:CommandField   ButtonType="Button"
                                      CancelText="C"
                                      ShowCancelButton="true"
                                      EditText="E"
                                      ShowEditButton="true"
                                      ControlStyle-Width="15px"
                                      ControlStyle-Font-Size="10px"
                                      ControlStyle-ForeColor="Blue"
                                      UpdateText="U"  />
          
                      
          
          
                      
                      <asp:TemplateField HeaderText="Coupon Name">
          
          A Offline
          A Offline
          ali_reza_zareian
          wrote on last edited by
          #4

          I think you forgot to use text property. CouponName.text="";

          H 1 Reply Last reply
          0
          • A ali_reza_zareian

            I think you forgot to use text property. CouponName.text="";

            H Offline
            H Offline
            haleemasher
            wrote on last edited by
            #5

            thnx 4 suggestion but i want to update a gridview and CouponName is my dropdownlist in gridview which i want to update.

            1 Reply Last reply
            0
            • H haleemasher

              When i run the code below it show error in bold line of code that "Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.TextBox'" as i m new to C# programming so i can't resolve it.

              protected void gvCouponDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
              {
              int id;
              int CouponTypeID;
              int Serial_Start;
              int serial_End;
              TextBox CouponName;
              DateTime TranDate;

              GridViewRow row = gvCouponDetails.Rows\[e.RowIndex\];
              
              id = int.Parse(((Label)row.Cells\[0\].FindControl("lblID")).Text);
              TranDate = DateTime.Parse(((TextBox)row.Cells\[2\].FindControl("tbDate")).Text);
              

              ** CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();**
              Serial_Start= int.Parse(((TextBox)row.Cells[4].FindControl("tbSerialStart")).Text);
              serial_End = int.Parse(((TextBox)row.Cells[5].FindControl("tbSerialEnd")).Text);

              string UpdateQuery = string.Format("update Coupon\_Info Set ReceivingDate = '{0}',Coupon\_TypeID = {1}, Serial\_Start={2},Serial\_End={3} where ID= {4}",TranDate.ToString(),CouponTypeID.ToString(),Serial\_Start.ToString(),serial\_End.ToString(),id.ToString());
              DAL da = new DAL();
              da.ConnectionString = ConfigurationManager.AppSettings\["ConnectionString"\].ToString();
              int i= da.ExecuteNonQuery(CommandType.Text, UpdateQuery, null);
              gvCouponDetails.EditIndex = -1;
              boundgridview();
              

              }

              ASP code is

              <asp:GridView ID="gvCouponDetails"
              runat="server"
              AutoGenerateColumns="false"
              onrowcancelingedit="gvCouponDetails_RowCancelingEdit"
              onrowediting="gvCouponDetails_RowEditing"
              onrowupdating="gvCouponDetails_RowUpdating"
              DataKeyNames="ID"
              style="top: 521px; left: 276px; position: absolute; height: 143px; width: 704px">
              <Columns>

                      <asp:CommandField   ButtonType="Button"
                                          CancelText="C"
                                          ShowCancelButton="true"
                                          EditText="E"
                                          ShowEditButton="true"
                                          ControlStyle-Width="15px"
                                          ControlStyle-Font-Size="10px"
                                          ControlStyle-ForeColor="Blue"
                                          UpdateText="U"  />
              
                          
              
              
                          
                          <asp:TemplateField HeaderText="Coupon Name">
              
              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              haleemasher wrote:

              as i m new to C# programming

              OK, well, given that the error message is in plain english, I'm not sure what to suggest. If you knew how to program, I'd suggest you break this up into as many lines as you can and step through in the debugger to work out what is going wrong. As you don't know C#, I assume you're either in a class, or teaching yourself. If you're in a class, you should talk to your teacher. If you're teaching yourself, buy a basic book on C#, BEFORE you try to learn ASP.NET. And if this is paid work, you are an utter disgrace, and should tell your client right now that you are stealing from them. If you work for a company, I'd expect them to go broke soon, hiring people like you. Your best way forward would be to buy a C# book and work through it, in the hope that when they fail and you look for another programming job, you find yourself actually capable of doing some programming and/or understanding basic error messages. I assume you didn't write this code. Whoever did, isn't very good. It's horrible. CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString(); There are at least four ways that this code could blow up, it should be on at least 4 lines, with extra code to check the assumptions being made.

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

              1 Reply Last reply
              0
              • H haleemasher

                When i run the code below it show error in bold line of code that "Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.TextBox'" as i m new to C# programming so i can't resolve it.

                protected void gvCouponDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
                {
                int id;
                int CouponTypeID;
                int Serial_Start;
                int serial_End;
                TextBox CouponName;
                DateTime TranDate;

                GridViewRow row = gvCouponDetails.Rows\[e.RowIndex\];
                
                id = int.Parse(((Label)row.Cells\[0\].FindControl("lblID")).Text);
                TranDate = DateTime.Parse(((TextBox)row.Cells\[2\].FindControl("tbDate")).Text);
                

                ** CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();**
                Serial_Start= int.Parse(((TextBox)row.Cells[4].FindControl("tbSerialStart")).Text);
                serial_End = int.Parse(((TextBox)row.Cells[5].FindControl("tbSerialEnd")).Text);

                string UpdateQuery = string.Format("update Coupon\_Info Set ReceivingDate = '{0}',Coupon\_TypeID = {1}, Serial\_Start={2},Serial\_End={3} where ID= {4}",TranDate.ToString(),CouponTypeID.ToString(),Serial\_Start.ToString(),serial\_End.ToString(),id.ToString());
                DAL da = new DAL();
                da.ConnectionString = ConfigurationManager.AppSettings\["ConnectionString"\].ToString();
                int i= da.ExecuteNonQuery(CommandType.Text, UpdateQuery, null);
                gvCouponDetails.EditIndex = -1;
                boundgridview();
                

                }

                ASP code is

                <asp:GridView ID="gvCouponDetails"
                runat="server"
                AutoGenerateColumns="false"
                onrowcancelingedit="gvCouponDetails_RowCancelingEdit"
                onrowediting="gvCouponDetails_RowEditing"
                onrowupdating="gvCouponDetails_RowUpdating"
                DataKeyNames="ID"
                style="top: 521px; left: 276px; position: absolute; height: 143px; width: 704px">
                <Columns>

                        <asp:CommandField   ButtonType="Button"
                                            CancelText="C"
                                            ShowCancelButton="true"
                                            EditText="E"
                                            ShowEditButton="true"
                                            ControlStyle-Width="15px"
                                            ControlStyle-Font-Size="10px"
                                            ControlStyle-ForeColor="Blue"
                                            UpdateText="U"  />
                
                            
                
                
                            
                            <asp:TemplateField HeaderText="Coupon Name">
                
                P Offline
                P Offline
                padmanabhan N
                wrote on last edited by
                #7

                haleemasher wrote:

                CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();

                insead use: string CouponName = string.empty; CouponName = ((DropDownList)row.Cells[3].FindControl("ddlCoupon")).Text;

                Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]

                C 1 Reply Last reply
                0
                • P padmanabhan N

                  haleemasher wrote:

                  CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();

                  insead use: string CouponName = string.empty; CouponName = ((DropDownList)row.Cells[3].FindControl("ddlCoupon")).Text;

                  Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  I have no idea how a textbox is getting involved in his code, but I suspect that Cells[3] has a textbox in it, instead of a drop down. He needs to break this in to steps and use the debugger to work out what is wrong. If he's capable of it.

                  Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                  1 Reply Last reply
                  0
                  • A Abhijit Jana

                    haleemasher wrote:

                    TextBox CouponName;

                    Why CoupanName is TextBox Type ?

                    haleemasher wrote:

                    CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();

                    Use

                    String CouponName;
                    CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();

                    Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

                    S Offline
                    S Offline
                    Sendilkumar M
                    wrote on last edited by
                    #9

                    CouponName will always contains some string value.Casting it to TextBox is meaningless.If you want to have TextBox based on the value of CouponName use some control logic to get it done.

                    M.Sendilkumar

                    1 Reply Last reply
                    0
                    • H haleemasher

                      When i run the code below it show error in bold line of code that "Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.TextBox'" as i m new to C# programming so i can't resolve it.

                      protected void gvCouponDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
                      {
                      int id;
                      int CouponTypeID;
                      int Serial_Start;
                      int serial_End;
                      TextBox CouponName;
                      DateTime TranDate;

                      GridViewRow row = gvCouponDetails.Rows\[e.RowIndex\];
                      
                      id = int.Parse(((Label)row.Cells\[0\].FindControl("lblID")).Text);
                      TranDate = DateTime.Parse(((TextBox)row.Cells\[2\].FindControl("tbDate")).Text);
                      

                      ** CouponName =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();**
                      Serial_Start= int.Parse(((TextBox)row.Cells[4].FindControl("tbSerialStart")).Text);
                      serial_End = int.Parse(((TextBox)row.Cells[5].FindControl("tbSerialEnd")).Text);

                      string UpdateQuery = string.Format("update Coupon\_Info Set ReceivingDate = '{0}',Coupon\_TypeID = {1}, Serial\_Start={2},Serial\_End={3} where ID= {4}",TranDate.ToString(),CouponTypeID.ToString(),Serial\_Start.ToString(),serial\_End.ToString(),id.ToString());
                      DAL da = new DAL();
                      da.ConnectionString = ConfigurationManager.AppSettings\["ConnectionString"\].ToString();
                      int i= da.ExecuteNonQuery(CommandType.Text, UpdateQuery, null);
                      gvCouponDetails.EditIndex = -1;
                      boundgridview();
                      

                      }

                      ASP code is

                      <asp:GridView ID="gvCouponDetails"
                      runat="server"
                      AutoGenerateColumns="false"
                      onrowcancelingedit="gvCouponDetails_RowCancelingEdit"
                      onrowediting="gvCouponDetails_RowEditing"
                      onrowupdating="gvCouponDetails_RowUpdating"
                      DataKeyNames="ID"
                      style="top: 521px; left: 276px; position: absolute; height: 143px; width: 704px">
                      <Columns>

                              <asp:CommandField   ButtonType="Button"
                                                  CancelText="C"
                                                  ShowCancelButton="true"
                                                  EditText="E"
                                                  ShowEditButton="true"
                                                  ControlStyle-Width="15px"
                                                  ControlStyle-Font-Size="10px"
                                                  ControlStyle-ForeColor="Blue"
                                                  UpdateText="U"  />
                      
                                  
                      
                      
                                  
                                  <asp:TemplateField HeaderText="Coupon Name">
                      
                      H Offline
                      H Offline
                      harshangrana
                      wrote on last edited by
                      #10

                      Try this CouponName.Text =((DropDownList)row.Cells[3].FindControl("ddlCoupon")).SelectedValue.ToString();

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups