Click here to Skip to main content
15,898,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem with my code.i am selecting a row from a grid view using radio button and inserting the data into another table.But while inserting i am getting a error like Input string was not in a correct format.my code is below.Please anyone help resolving this error.
C#
 protected void txtorderitem_Click(object sender, EventArgs e)
{
        string sno=null;
        RadioButton check = new RadioButton();
        foreach (GridViewRow dgi in grdfirmord.Rows)
        {

            check = (RadioButton)dgi.FindControl("rdbtn");
            if (check != null)
            {
                if (check.Checked)
                {
                    check.Checked = true;
                    sno = grdfirmord.DataKeyNames.ToString();
                }
            }
        }
        stPeriodicalDet obGet = new stPeriodicalDet();
        obGet.OrderNo = clsDbCommon.ORDAutoNO("OrderNo", "PeriodicalOrderDetails").ToString();
        obGet.SubscriptionNo = Convert.ToInt32(sno.ToString());
        Int32 Succ = Periodicalcls.InsertPeriodicalOrderDetails(ref obGet);
}

i am getting error in obGet.Subscription=....
Thank you
Posted
Updated 11-Aug-13 20:52pm
v3
Comments
Syed Shabeer 12-Aug-13 2:54am    
Can you debug and find out what actually are you getting in "sno = grdfirmord.DataKeyNames.ToString();" ?

The ToString() method of the sno variable is producing a string that the .NET framework does not know how to convert to an Int.

Try to debug,
stop at the line:
C#
sno = grdfirmord.DataKeyNames.ToString();

or at
C#
obGet.SubscriptionNo = Convert.ToInt32(sno.ToString());

and see what the value of sno is, you will probably pretty soon find the issue.

* BTW, sno is already a string, and you convert it to string again in the second line there.

Cheers,
Edo
 
Share this answer
 
Quote:
obGet.SubscriptionNo = Convert.ToInt32(sno.ToString());

In this line you have:
  1. Called ToString() on a string.
  2. Called Convert.ToInt32 without providing the appropriate exception-handling code.


Point 1 is a minor flaw, you are just doing a useless call.
Point 2, on the other hand should be fixed, since you are never sure that input string is in a correct numeric format. You have either to:
  • Provide the exception handling code.
or
  • call the Int32.TryParse[^] method instead of Convert.ToInt32 (you have, of course, to check TryParse return value, handling failures).
 
Share this answer
 
I think the code
XML
<pre>obGet.SubscriptionNo = Convert.ToInt32(sno.ToString()); genrate the error
if radio button not selected

then sno=null;</pre>
and
obGet.SubscriptionNo = Convert.ToInt32(sno.ToString()); gives the error



if it helps please select asnswer..
 
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