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
  1. Home
  2. Web Development
  3. ASP.NET
  4. different ValueField and TextField for a dropdownlist

different ValueField and TextField for a dropdownlist

Scheduled Pinned Locked Moved ASP.NET
databasesysadminhelp
7 Posts 4 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

    i have a dropdownlist i bind it such like that it has different valuefield and different textfield. i want that i select textfield from dropdown and the value correspond that valuefield will insert in the database i write a code for it, it show datatype error.Failed to convert parameter value from string to a byte

    <asp:DropDownList ID="ddlCType" runat="server"
    DataSourceID="SqlDataSource1" DataTextField="CouponName"
    DataValueField="CouponTypeID"

        style="top: 309px; left: 389px; position: absolute; height: 11px; width: 97px; z-index: 1" 
        AutoPostBack="True" ontextchanged="DropDownList1\_TextChanged">
    

    </asp:DropDownList>

        try
        {
            ErrorMessage.Text = "";
            cnx.Open();
            SqlCommand insertCmd = new SqlCommand("insert into CouponTransaction(CouponTypeID)values(@Id)", cnx);
            insertCmd.Parameters.Add("@Id", SqlDbType.TinyInt);
            insertCmd.Parameters\["@Id"\].Value = ddlCType.Text;insertCmd.ExecuteNonQuery();
            ErrorMessage.Text = "Records inserted successfully";
            cnx.Close();
        }
    
    S U V 3 Replies Last reply
    0
    • H haleemasher

      i have a dropdownlist i bind it such like that it has different valuefield and different textfield. i want that i select textfield from dropdown and the value correspond that valuefield will insert in the database i write a code for it, it show datatype error.Failed to convert parameter value from string to a byte

      <asp:DropDownList ID="ddlCType" runat="server"
      DataSourceID="SqlDataSource1" DataTextField="CouponName"
      DataValueField="CouponTypeID"

          style="top: 309px; left: 389px; position: absolute; height: 11px; width: 97px; z-index: 1" 
          AutoPostBack="True" ontextchanged="DropDownList1\_TextChanged">
      

      </asp:DropDownList>

          try
          {
              ErrorMessage.Text = "";
              cnx.Open();
              SqlCommand insertCmd = new SqlCommand("insert into CouponTransaction(CouponTypeID)values(@Id)", cnx);
              insertCmd.Parameters.Add("@Id", SqlDbType.TinyInt);
              insertCmd.Parameters\["@Id"\].Value = ddlCType.Text;insertCmd.ExecuteNonQuery();
              ErrorMessage.Text = "Records inserted successfully";
              cnx.Close();
          }
      
      S Offline
      S Offline
      Suresh Suthar
      wrote on last edited by
      #2

      haleemasher wrote:

      insertCmd.Parameters["@Id"].Value = ddlCType.Text;

      You should use ddlCType.SelectedValue property for dropdownlist's selected item's value. :laugh:

      Be an Eagle, Sky is Yours.

      H 1 Reply Last reply
      0
      • S Suresh Suthar

        haleemasher wrote:

        insertCmd.Parameters["@Id"].Value = ddlCType.Text;

        You should use ddlCType.SelectedValue property for dropdownlist's selected item's value. :laugh:

        Be an Eagle, Sky is Yours.

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

        Same error may i change this code also

                insertCmd.Parameters.Add("@Id", SqlDbType.TinyInt);
        
        1 Reply Last reply
        0
        • H haleemasher

          i have a dropdownlist i bind it such like that it has different valuefield and different textfield. i want that i select textfield from dropdown and the value correspond that valuefield will insert in the database i write a code for it, it show datatype error.Failed to convert parameter value from string to a byte

          <asp:DropDownList ID="ddlCType" runat="server"
          DataSourceID="SqlDataSource1" DataTextField="CouponName"
          DataValueField="CouponTypeID"

              style="top: 309px; left: 389px; position: absolute; height: 11px; width: 97px; z-index: 1" 
              AutoPostBack="True" ontextchanged="DropDownList1\_TextChanged">
          

          </asp:DropDownList>

              try
              {
                  ErrorMessage.Text = "";
                  cnx.Open();
                  SqlCommand insertCmd = new SqlCommand("insert into CouponTransaction(CouponTypeID)values(@Id)", cnx);
                  insertCmd.Parameters.Add("@Id", SqlDbType.TinyInt);
                  insertCmd.Parameters\["@Id"\].Value = ddlCType.Text;insertCmd.ExecuteNonQuery();
                  ErrorMessage.Text = "Records inserted successfully";
                  cnx.Close();
              }
          
          U Offline
          U Offline
          Usharva
          wrote on last edited by
          #4

          Hi, insertCmd.Parameters["@Id"].Value = ddlCType.Text;insertCmd.ExecuteNonQuery(); replace the above code with insertCmd.Parameters["@Id"].Value = int.Parse(ddlCType.SelectedValue);insertCmd.ExecuteNonQuery();

          1 Reply Last reply
          0
          • H haleemasher

            i have a dropdownlist i bind it such like that it has different valuefield and different textfield. i want that i select textfield from dropdown and the value correspond that valuefield will insert in the database i write a code for it, it show datatype error.Failed to convert parameter value from string to a byte

            <asp:DropDownList ID="ddlCType" runat="server"
            DataSourceID="SqlDataSource1" DataTextField="CouponName"
            DataValueField="CouponTypeID"

                style="top: 309px; left: 389px; position: absolute; height: 11px; width: 97px; z-index: 1" 
                AutoPostBack="True" ontextchanged="DropDownList1\_TextChanged">
            

            </asp:DropDownList>

                try
                {
                    ErrorMessage.Text = "";
                    cnx.Open();
                    SqlCommand insertCmd = new SqlCommand("insert into CouponTransaction(CouponTypeID)values(@Id)", cnx);
                    insertCmd.Parameters.Add("@Id", SqlDbType.TinyInt);
                    insertCmd.Parameters\["@Id"\].Value = ddlCType.Text;insertCmd.ExecuteNonQuery();
                    ErrorMessage.Text = "Records inserted successfully";
                    cnx.Close();
                }
            
            V Offline
            V Offline
            Viral Upadhyay
            wrote on last edited by
            #5

            haleemasher wrote:

            insertCmd.Parameters["@Id"].Value = ddlCType.Text

            Use insertCmd.Parameters["@Id"].Value = Convert.ToByte(ddlCType.SelectedValue); Viral

            Viral My Site Tips & Tracks

            H 1 Reply Last reply
            0
            • V Viral Upadhyay

              haleemasher wrote:

              insertCmd.Parameters["@Id"].Value = ddlCType.Text

              Use insertCmd.Parameters["@Id"].Value = Convert.ToByte(ddlCType.SelectedValue); Viral

              Viral My Site Tips & Tracks

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

              it show another error Input sting was not in correct format

              V 1 Reply Last reply
              0
              • H haleemasher

                it show another error Input sting was not in correct format

                V Offline
                V Offline
                Viral Upadhyay
                wrote on last edited by
                #7

                sorry my mistake Use insertCmd.Parameters["@Id"].Value = Convert.Int32(ddlCType.SelectedValue); here you can use Int32 or Int16 depends on your requirement. Hope this help Viral

                Viral My Site Tips & Tracks

                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