Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
4.25/5 (6 votes)
See more:
I have a dropdown list that has a different datafield(CouponName) and datavalue(CouponTypeID). I want the code to work so that when I select a datafield it saves datavalue in database but it shows an error::

Input string was not in a correct format

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        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 =
                Int32.Parse (ddlCType.SelectedValue);
           
          
            insertCmd.ExecuteNonQuery();
            ErrorMessage.Text = "Records inserted successfully";
            cnx.Close();
        }
        catch (Exception ex)
        {
            ErrorMessage.Text = ex.Message;
        }
Posted
Updated 19-Mar-10 11:51am
v3
Comments
Christian Graus 3-Jun-10 23:03pm    
Seriously, why would someone edit or reply to a question from 6 months ago ? If he's not solved it now, he's never going to, people.

the below code never makes issue. And try to use int.tryParse instread of
int.parse.

tryParse is performing with very less conversion time.


protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
	    int cardTypeId = 0;
            if (ddlCType.selectedIndex >=0)
            {
            int.tryParse(ddlCType.SelectedValue.toString(),out cardTypeId);
           }
            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 =  cardTypeId;
           
          
            insertCmd.ExecuteNonQuery();
            ErrorMessage.Text = "Records inserted successfully";
            cnx.Close();
        }
        catch (Exception ex)
        {
            ErrorMessage.Text = ex.Message;
        }
   }



Happy coding
 
Share this answer
 
Good day haleemasher

Change your code to

protected void btnSubmit_Click(object sender, EventArgs e)<br />    {<br />        try<br />        {<br />            ErrorMessage.Text = "";<br />            cnx.Open();<br />            SqlCommand insertCmd = new SqlCommand("insert into CouponTransaction(CouponTypeID)values(@id)", cnx);<br />                        <br />            insertCmd.Parameters.Add("@Id", SqlDbType.int);<br />            insertCmd.Parameters["@Id"].Value = Convert.ToInt32(ToddlCType.SelectedValue);<br />                   <br />            insertCmd.ExecuteNonQuery();<br />            ErrorMessage.Text = "Records inserted successfully";<br />            cnx.Close();<br />        }<br />        catch (Exception ex)<br />        {<br />            ErrorMessage.Text = ex.Message;<br />        }


You must put a break point and check what results you get from the selected value. it might have returned an empty string that is why you got that error. You must stop assuming the input from the user is correct.

Try it and tell me what it does

 
Share this answer
 
Use try parse and check for the value.
 
Share this answer
 
Are you doing something like this

C#
protected void Page_Load(object sender, EventArgs e)
        {
            BindList();
        }



In this case the selected value will be reset as the list gets binded againg before the buttone click event takes place.

Make sure that you bind you list only once, not in each and every postback like following.

C#
protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              BindList();
          }
      }
 
Share this answer
 
In the dropdown list give Dropdownlist selected text and selected value.

set ur Dropdownlist Selected Text:CouponTypeText
and selected Value:CouponTypeID
 
Share this answer
 
Have you add DataValueField,DataTextField in your dropdownlist:

XML
<asp:DropDownList ID="DropDownList1" runat="server" DataValueField="CouponTypeID"
         DataTextField="CouponTypeText">
        </asp:DropDownList>
 
Share this answer
 
Interesting Question, good answers, shame it is 15 months old.

Why did this get answered here?

Moderators to Red Alert?
 
Share this answer
 
For the record, it's WRONG to say that tryparse is quicker. It's only quicker if the attempt is not valid, because it does not throw an exception. The reason to use it, is to make sure the input is valid.
 
Share this answer
 
hi,

first correct ur query, you didnt mention the table name in ur query.
"insert into CouponTransaction(CouponTypeID)values(@id)"
 
Share this answer
 
try this one
"insert into CouponTransaction(CouponTypeID)"+"Values(@id)"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900