Hi,
i am trying to accumulate a total to be stored in my 'targetTotal' variable of type int which is then to be sent to my msaccess db into a field called 'targetTotal' of type LongInteger, the trouble i am having now is that after adding my totals given the following code snippet:
targetOneDay = Convert.ToInt32(nine_to_fourteenhrstxbx.Text);//tonine_to_fourteenhrstxbx.Text;
targetTwoDay = Convert.ToInt32(fifteen_to_fortfourglhtxtbx.Text);
targetFiveDay = Convert.ToInt32(fourtyfive_to_74txtbx.Text);
targetTenDay = Convert.ToInt32(min75glhtxtbx.Text);
targetTotal = Convert.ToInt32(totaltargettxtbx.Text);
targetTotal = targetOneDay + targetTwoDay + targetFiveDay + targetTenDay;
i get an error which says the following: 'Input string was not in correct format' pointing to the target total variable. So i looked online for this particular error and based on my understanding i have to check if it is a null value and convert it accordingly. So what i first did was created another variable called 'total' of type int, removed the 'text' property of my target total and added the following if statement:
targetTotal = Convert.ToInt32(totaltargettxtbx);
if (targetTotal != null)
total = Convert.ToInt32(targetTotal);
else
total = 0;
now when i run my applicantion i get another error saying: Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'System.IConvertible'. which is pointing to my targetTotal variable.
Can somebody kindly assist me with my issue please.
Loading

Subhendu DeRank #1,108 · 1.7K reputationPosted Dec 11, 2010, 8:53 AM EST
if(!nine_to_fourteenhrstxbx.Text.Trim().Equals(String.Empty))
targetOneDay = Convert.ToInt32(nine_to_fourteenhrstxbx.Text);
else
targetOneDay=0;
if(!fifteen_to_fortfourglhtxtbx.Text.Trim().Equals(String.Empty))
targetTwoDay = Convert.ToInt32(fifteen_to_fortfourglhtxtbx.Text);
else
targetTwoDay = 0;
if(!fourtyfive_to_74txtbx.Text.Trim().Equals(String.Empty))
targetFiveDay = Convert.ToInt32(fourtyfive_to_74txtbx.Text);
else
targetFiveDay = 0;
if(!min75glhtxtbx.Text.Trim().Equals(String.Empty))
targetTenDay = Convert.ToInt32(min75glhtxtbx.Text);
else
targetTenDay = 0;
if(!totaltargettxtbx.Text.Trim().Equals(String.Empty))
targetTotal = Convert.ToInt32(totaltargettxtbx.Text);
else
targetTotal = 0;
targetTotal = targetOneDay + targetTwoDay + targetFiveDay + targetTenDay;
Thanks.....
Suthish NairRank #92 · 22.8K reputationPosted Dec 11, 2010, 10:32 AM EST
targetOneDay = (nine_to_fourteenhrstxbx.Text.ToString().Trim() == "" ? 0 : Convert.ToInt32(nine_to_fourteenhrstxbx.Text)); targetTwoDay = (nine_to_fourteenhrstxbx.Text.ToString().Trim() == "" ? 0 : Convert.ToInt32(fifteen_to_fortfourglhtxtbx.Text)); targetFiveDay = (nine_to_fourteenhrstxbx.Text.ToString().Trim() == "" ? 0 : Convert.ToInt32(fourtyfive_to_74txtbx.Text)); targetTenDay = (nine_to_fourteenhrstxbx.Text.ToString().Trim() == "" ? 0 : Convert.ToInt32(min75glhtxtbx.Text)); targetTotal = (nine_to_fourteenhrstxbx.Text.ToString().Trim() == "" ? 0 : Convert.ToInt32(totaltargettxtbx.Text));Sam HobbsRank #107 · 20.5K reputationPosted Dec 11, 2010, 6:35 PM EST
I assume that totaltargettxtbx is a TextBox. If so then that is why you are getting the message "Unable to cast".... since totaltargettxtbx is not a string.
So you need to use totaltargettxtbx.Text. Also, it is totaltargettxtbx.Text that you need to check for null. It is irrelevant (not useful) to check targetTotal for null.
simon taylor38 reputationPosted Dec 12, 2010, 3:44 AM EST
thanks for the reply guys, did mange to try out Subhendu De's answer which worked fine, BUT i when i used Sam Hobbs answer, this is what i did:
targetTotal = Convert.ToInt32(totaltargettxtbx.Text); // error pointing here
targetTotal += targetOneDay + targetTwoDay + targetFiveDay + targetTenDay;
if (totaltargettxtbx.Text != null)
total = Convert.ToInt32(targetTotal);
else
total = 0;
i get the following error message: Input string was not in a correct format. which pionts to the tartTotal variable line. Yes, it is a textbox, when i hover my mouse over the variables, i see the values but when hovering over the targetTotal variable it does not show the result. Although the other solution does work, I would still be greatful if you could still assist me with this Sam Hobbs beceause if i get this same issue 6 months down the line, i have more ammo in my arsenal as it were to deal with this type of problem.
Sam HobbsRank #107 · 20.5K reputationPosted Dec 12, 2010, 7:56 AM EST
Why are you doing:
That does not make sense to me, since targetTotal is not a string; correct?
Suthish NairRank #92 · 22.8K reputationPosted Dec 12, 2010, 8:11 AM EST
did you tried the code i posted.
simon taylor38 reputationPosted Dec 12, 2010, 9:04 AM EST
total = Convert.ToInt32(targetTotal);
because in my ms access db the field name targetTotal is of type Long Integer, and i believe i would need to convert it to a 32-bit value inorder to send it to my db. Yes, targetTotal targetTotal as it's of type int.
simon taylor38 reputationPosted Dec 12, 2010, 9:34 AM EST
i did actaully try your code BUT there was an error message of :Input string was not in a correct format. pionting to this line: targetTotal = (nine_to_fourteenhrstxbx.Text.ToString().Trim() == "" ? 0 : Convert.ToInt32(totaltargettxtbx.Text));
Sam HobbsRank #107 · 20.5K reputationPosted Dec 12, 2010, 10:01 AM EST
simon taylor38 reputationPosted Dec 12, 2010, 10:24 AM EST
private void submitbtn_Click(object sender, EventArgs e)
{
string projectName;
string startDate;
string endDate;
int targetOneDay =0;
int targetTwoDay = 0;
int targetFiveDay = 0;
int targetTenDay = 0;
int targetTotal =0;
int total = 0;
i also want to point out that before i added the targetTotal thing, i was able to send the resulst of all my other targetXXDay's to my ms access database since adding the targetTotal stuff it all went down hill. Hope that helps.
Sam HobbsRank #107 · 20.5K reputationPosted Dec 12, 2010, 10:27 AM EST
simon taylor38 reputationPosted Dec 12, 2010, 10:46 AM EST
simon taylor38 reputationPosted Dec 12, 2010, 11:41 AM EST
private void submitbtn_Click(object sender, EventArgs e)
{
string projectName;
string startDate;
string endDate;
int targetOneDay =0;
int targetTwoDay = 0;
int targetFiveDay = 0;
int targetTenDay = 0;
int targetTotal =0;
int total = 0;
string dbinsert;
projectName = Convert.ToString(nametxtbx.Text);//nametxtbx.Text;
startDate = startdatetimepcker.Value.ToString();
endDate = enddatetimepcker.Value.ToString();
targetOneDay = Convert.ToInt32(nine_to_fourteenhrstxbx.Text);
targetTwoDay = Convert.ToInt32(fifteen_to_fortfourglhtxtbx.Text);
targetFiveDay = Convert.ToInt32(fourtyfive_to_74txtbx.Text);
targetTenDay = Convert.ToInt32(min75glhtxtbx.Text);
targetTotal += targetOneDay + targetTwoDay + targetFiveDay + targetTenDay;
if (totaltargettxtbx.Text != null)
total = targetTotal;
else
total = 0;
targetTotal = Convert.ToInt32(totaltargettxtbx.Text);
cmd = null;
dbinsert = "INSERT into Project(projectName, startDate, endDate, duration, targetOne, targetTwo, targetFive, targetTen, targetTotal)" +
"VALUES" + "("+"'"+projectName+"'"+"," + "'" + startDate+"'"+","+"'"+endDate+"'"+","+"5"+","+targetOneDay+"," + targetTwoDay+","+targetFiveDay+","+targetTenDay+","+total+ ")";
cmd = new OleDbCommand(dbinsert, con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
}
although i'm still getting the same issue
Suthish NairRank #92 · 22.8K reputationPosted Dec 12, 2010, 11:59 AM EST
targetOneDay = (nine_to_fourteenhrstxbx.Text.ToString().Trim() == "" ? 0 : Convert.ToInt32(nine_to_fourteenhrstxbx.Text)); targetTwoDay = (fifteen_to_fortfourglhtxtbx.Text.ToString().Trim() == "" ? 0 : Convert.ToInt32(fifteen_to_fortfourglhtxtbx.Text)); targetFiveDay = (fourtyfive_to_74txtbx.Text.ToString().Trim() == "" ? 0 : Convert.ToInt32(fourtyfive_to_74txtbx.Text)); targetTenDay = (min75glhtxtbx.Text.ToString().Trim() == "" ? 0 : Convert.ToInt32(min75glhtxtbx.Text));Sam HobbsRank #107 · 20.5K reputationPosted Dec 12, 2010, 2:29 PM EST
Note that I also reorganized the way you generate the insert command. It is a recommendation; you don't have to do it that way, but I think it makes things easier. If you do it my way, then you can put the dbinsertFormat string someplace else where it is out of the way. Note that you probably need to also change the way the dates are specified, but I am not sure of the details; others can help you more with that than I can. Also, it is possible ot create a command using parameters; I think that shold be the next thing for you to learn.
simon taylor38 reputationPosted Dec 13, 2010, 3:17 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Dec 13, 2010, 3:52 AM EST