Forums » .NET » .NET »

Input string was not in a correct format


Posted Date: 22 Jul 2012      Posted By:: ptina     Member Level: Bronze    Member Rank: 1584     Points: 5   Responses: 3



private void btn_Clear_Click(object sender, EventArgs e)
{
txt_ID.Text = "";
txt_Manufacturer.Text = "";
txt_CatNo.Text = "";
txt_DischargeRate1.Text = "";
txt_BatteryCapacity.Text = "";
txt_Voltage.Text = "";
txt_Weight.Text = "";
txt_Length.Text = "";
txt_Width.Text = "";
txt_Height.Text = "";
txt_Series.Text = "";
txt_Parallel.Text = "";
//txt_TotalBatteries.Text = "";
}

private void btn_Exit_Click(object sender, EventArgs e)
{
this.Close();
}

private void txt_SystemVoltage_TextChanged(object sender, EventArgs e)
{
int intBatterySeries;
double dblBatterySeries;

if (txt_SystemVoltage.Text != "" && txt_Voltage.Text != "")
{
dblBatterySeries = Convert.ToDouble(txt_SystemVoltage.Text) / Convert.ToDouble(txt_Voltage.Text);
intBatterySeries = Convert.ToInt32(dblBatterySeries);
txt_Series.Text = intBatterySeries.ToString();
}

}

private void txt_Voltage_TextChanged(object sender, EventArgs e)
{
int intBatterySeries;
double dblBatterySeries;

if (txt_SystemVoltage.Text != "" && txt_Voltage.Text != "")
{
dblBatterySeries = Convert.ToDouble(txt_SystemVoltage.Text) / Convert.ToDouble(txt_Voltage.Text);
intBatterySeries = Convert.ToInt32(dblBatterySeries);
txt_Series.Text = intBatterySeries.ToString();
}


}

private void txt_Amp_TextChanged(object sender, EventArgs e)
{
int intBatteryParallel;
double dblBatteryParallel;

if (txt_SystemVoltage.Text != "" && txt_Voltage.Text != "")
{
dblBatteryParallel = Convert.ToDouble(txt_Amp.Text) / Convert.ToDouble(txt_BatteryCapacity.Text);
intBatteryParallel = Convert.ToInt32(dblBatteryParallel);
txt_Parallel.Text = intBatteryParallel.ToString();
}
}

private void txt_BatteryCapacity_TextChanged(object sender, EventArgs e)
{
int intBatteryParallel;
double dblBatteryParallel;//<<<error msg

if (txt_SystemVoltage.Text != "" && txt_Voltage.Text != "")
{
dblBatteryParallel = Convert.ToDouble(txt_Amp.Text) / Convert.ToDouble(txt_BatteryCapacity.Text);//<<<<<<<<<<<<<<<<<<<<<<< error
intBatteryParallel = Convert.ToInt32(dblBatteryParallel);
txt_Parallel.Text = intBatteryParallel.ToString();
}
}

private void textBox2_TextChanged_1(object sender, EventArgs e)
{

}

private void txt_TotalBatteries_TextChanged(object sender, EventArgs e)
{
int intBatteryParallel;
double dblTotalBattery;

if (txt_Series.Text != "0" && txt_Parallel.Text != "0")
{
dblTotalBattery = Convert.ToDouble(txt_Series.Text) * Convert.ToDouble(txt_Parallel.Text);
intBatteryParallel = Convert.ToInt32(dblTotalBattery);
txt_TotalBatteries.Text = intBatteryParallel.ToString();
}
}
}
}


when i click the clear button. error msg as formatexception was unhandled. input string was not ina correct format.
at this part of the code as shown above.
private void txt_BatteryCapacity_TextChanged(object sender, EventArgs e)
{
int intBatteryParallel;
double dblBatteryParallel;//<<<error msg

if (txt_SystemVoltage.Text != "" && txt_Voltage.Text != "")
{
dblBatteryParallel = Convert.ToDouble(txt_Amp.Text) / Convert.ToDouble(txt_BatteryCapacity.Text);//<<<<<<<<<<<<<<<<<<<<<<< error
intBatteryParallel = Convert.ToInt32(dblBatteryParallel);
txt_Parallel.Text = intBatteryParallel.ToString();
}
}

1)how to correct the error as i am unable to clear the textboxes? 2)any mistake at txt_TotalBatteries as i am unable to get ans by multiplying txt_series and txt_parallel?




Responses

#681345    Author: ptina      Member Level: Bronze      Member Rank: 1584     Date: 22/Jul/2012   Rating: 2 out of 52 out of 5     Points: 1

sometimes i get values for txt_parallel as 0.2,0.5 but the ans is displayed as 0. how to display as 1 even the value is 0.2 or 0.5?


 
#681346    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 22/Jul/2012   Rating: 2 out of 52 out of 5     Points: 2

Hi,

Before converting to Double make sure txt_BatteryCapacity text is integer value if you pass string value then the Input string error occur

Regards
N.Ravindran
Your Hard work never fails



 
#681353    Author: Dharmaraj Nagarajan        Member Level: Gold      Member Rank: 13     Date: 22/Jul/2012   Rating: 2 out of 52 out of 5     Points: 3

Hi,
These error is because of either the value is not matching the format to the data type you wish to convert as Ravi suggested. One more thing before proceeding with the conversion, you need to check for the null value.


string Num;
num="";
if(num != "")
{
//put your conversion process here.
}


All is well.
Thanks,
Dharma
Editor,Mentor,MVM
Try and fail but don't fail and try
Me and DNS






 
Post Reply
You must Sign In to post a response.

Next : Getter setter vs get set?
Previous : Scroll bar scrolling up automatically when user selects text from dropdownlist in Fck edit
Return to Discussion Forum
Post New Message
Category:

Related Messages
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.