Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How To Solve Input String Format

string

  • Please log in to reply
10 replies to this topic

#1 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 05 July 2012 - 12:26 AM

	 private void DailyEnergyDemand_Click(object sender, EventArgs e)
	 {
		 double w;
		 int x;
		 if (txtDailyEnergyDemand.Text != "0" && cbbSystemVoltage.Text != "0")
		 {
		 w = Convert.ToDouble(txtDailyEnergyDemand.Text) * Convert.ToDouble(cbbSystemVoltage.Text);
		 x = Convert.ToInt32(w);
		 txtAhDemand1.Text = Convert.ToString(x);
		 }
	 }
	 private void cbbSystemVoltage_SelectedIndexChanged(object sender, EventArgs e)
	 {
		 double w;
		 int x;
		 if (txtDailyEnergyDemand.Text != "0" && cbbSystemVoltage.Text != "0")
		 {
		 w = Convert.ToDouble(txtDailyEnergyDemand.Text) * Convert.ToDouble(cbbSystemVoltage.Text);
		 x = Convert.ToInt32(w);
		 txtAhDemand1.Text = Convert.ToString(x);
		 }
	 }
	 private void cbbNumberOfDaysOfAutonomy1_SelectedIndexChanged(object sender, EventArgs e)
	 {
	
		 double z, y;
		 int v;
		 z = Convert.ToInt32(txtAhDemand1.Text) * Convert.ToInt32(cbbNumberOfDaysOfAutonomy1.Text); [color=#FF0000]//Input String was not in correct format [/color]frm error msg[/color]
		 y = z / Convert.ToDouble(DepthOfDischarge.Text);
		 v= Convert.ToInt32(y);
	 lblBatteryCapacity1.Text = Convert.ToString(v);
		
	 }
	 private void DepthOfDischarge_SelectedIndexChanged(object sender, EventArgs e)
	 {
		
		 double z, y;
		 int v;
		 z = Convert.ToInt32(txtAhDemand1.Text) * Convert.ToInt32(cbbNumberOfDaysOfAutonomy1.Text);
		 y = z / Convert.ToDouble(DepthOfDischarge.Text);
		 v= Convert.ToInt32(y);
		 lblBatteryCapacity1.Text = Convert.ToString(v);
		
	 }

how to correct the input string?
  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 05 July 2012 - 05:16 AM

which input string?
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 05 July 2012 - 06:26 AM

which input string?

z = Convert.ToInt32(txtAhDemand1.Text) * Convert.ToInt32(cbbNumberOfDaysOfAutonomy1.Text);
y = z / Convert.ToDouble(DepthOfDischarge.Text);

i get error msg from these strings as FormatException was unhandled.
how to correct the coding?
  • 0

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 05 July 2012 - 07:33 AM

What input you are getting FormatException? I mean for what values of txtAhDeman1.Text and cbbNumberOfDaysOfAutonomy1.Text you are getting expection?
  • 0

#5 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 05 July 2012 - 07:39 AM

z = Convert.ToInt32(txtAhDemand1.Text) * Convert.ToInt32(cbbNumberOfDaysOfAutonomy1.Text);y = z / Convert.ToDouble(DepthOfDischarge.Text);

What input you are getting FormatException? And, which line you are getting exception on?


I am unable to select a value for cbbNumberOfDaysOfAutonomy. When im about to enter a value frm comboBox "FormatException Unhandled" error msg shown.
  • 0

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 05 July 2012 - 07:44 AM

Yeah, I think cbbNumberOfDaysOfAutonomy is of type ComboBox, right? So to get the selected item value, you need to use SelectedItem property of ComboBox. However, you should check whether this value is set to null or not before the conversion. So the code should be like
if (cbbNumberOfDaysOfAutonomy1.SelectedItem != null) {

   z = Convert.ToInt32(txtAhDemand1.Text) * Convert.ToInt32(cbbNumberOfDaysOfAutonomy1.SelectedItem);

   y = z / Convert.ToDouble(DepthOfDischarge.Text);

}

  • 0

#7 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 05 July 2012 - 07:56 AM

all convert-methods should be handled wit try and catch... as it throws error if not convertable...
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#8 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 05 July 2012 - 07:59 AM

I still receive the same error msg. the whole prgm codings i ve posted earlier. Could help me identify the error?
  • 0

#9 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 05 July 2012 - 08:31 AM

I still receive the same error msg. the whole prgm codings i ve posted earlier. Could help me identify the error?

how do i edit?
  • 0

#10 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 05 July 2012 - 09:26 AM

Something like this. it's written directly in this post, so I can have made mistakes.
int t = 0, s = 0;

try {
  t = Convert.ToInt32(txtAhDemand1.Text) ;
} catch (Exception e) {
  MessageBox.Show(e.message);
 // handle the error
}
try {
  s = Convert.ToInt32(cbbNumberOfDaysOfAutonomy1.Text);
} catch (Exception e) {
  MessageBox.Show(e.message);
}
z = t * s;

  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#11 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 05 July 2012 - 01:02 PM

you have also the typarse methods, which will return a boolean value for you to know if your number was succesfuly converted or not
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download