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
Sam HobbsPosted Dec 13, 2010, 8:52 AM
simon taylorPosted Dec 13, 2010, 8:17 AM
Sam HobbsPosted Dec 12, 2010, 7:29 PM
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.
Suthish NairPosted Dec 12, 2010, 4:59 PM
simon taylorPosted Dec 12, 2010, 4:41 PM
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
simon taylorPosted Dec 12, 2010, 3:46 PM
Sam HobbsPosted Dec 12, 2010, 3:27 PM
simon taylorPosted Dec 12, 2010, 3:24 PM
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 HobbsPosted Dec 12, 2010, 3:01 PM
simon taylorPosted Dec 12, 2010, 2:34 PM
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));
simon taylorPosted Dec 12, 2010, 2:04 PM
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.
Suthish NairPosted Dec 12, 2010, 1:11 PM
did you tried the code i posted.
Sam HobbsPosted Dec 12, 2010, 12:56 PM
Why are you doing:
That does not make sense to me, since targetTotal is not a string; correct?
simon taylorPosted Dec 12, 2010, 8:44 AM
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 HobbsPosted Dec 11, 2010, 11:35 PM
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.
Suthish NairPosted Dec 11, 2010, 3:32 PM
Subhendu DePosted Dec 11, 2010, 1:53 PM
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.....