My text box always compare with '0' whatever number i enter - c#

I was trying to set a range of numbers at text box (CHWV.text) which will able to update my database base on my if else condition.
I tried to key into the text box from 0 to 100. then use int.TryParse to convert the text box value into an integer.
However, CHWTemp always get 0 whatever numbers i try enter.
The codes run perfectly,but i cannot get the CHWTemp i wanted after entering into the text box.
I not sure Is there anything i missed out?
sqlite_cmd = sqlite_conn.CreateCommand();
int CHWTemp;
int.TryParse(CHWV.Text,out CHWTemp);
try
{
sqlite_conn.Open();
if (CHWTemp >= 0 && CHWTemp <= 10)
{
sqlite_cmd.CommandText = ("UPDATE Temperature SET Temp = 25 where id=12");
sqlite_cmd.ExecuteNonQuery();
}
else if (CHWTemp >= 11 && CHWTemp <= 20)
{
sqlite_cmd.CommandText = ("UPDATE Temperature SET Temp = 24 Where id=12");
sqlite_cmd.ExecuteNonQuery();
}
else if (CHWTemp >= 21 && CHWTemp <= 40)
{
sqlite_cmd.CommandText = ("UPDATE Temperature SET Temp = 23 where id=12");
sqlite_cmd.ExecuteNonQuery();
}
else if (CHWTemp >= 41 && CHWTemp <= 60)
{
sqlite_cmd.CommandText = ("UPDATE Temperature SET Temp = 22 where id=12");
sqlite_cmd.ExecuteNonQuery();
}
else if (CHWTemp >= 61 && CHWTemp <= 80)
{
sqlite_cmd.CommandText = ("UPDATE Temperature SET Temp = 21 where id=12");
sqlite_cmd.ExecuteNonQuery();
}
else if (CHWTemp >= 81 && CHWTemp <= 100)
{
sqlite_cmd.CommandText = ("UPDATE Temperature SET Temp = 20 where id=12");
sqlite_cmd.ExecuteNonQuery();
}
sqlite_conn.Close();
}
catch ()
{
}
Any help is much appreciated. Thanks.

Before using the value of CHWTemp please check the return value of int.TryParse(CHWV.Text,out CHWTemp); If you are getting false than the conversion is not happening from string to int.
bool ifSuccess = int.TryParse(CHWV.Text,out CHWTemp);
if the value of ifSuccess is true then conversion from string to int is done but if the value of CHWV.Text is not convertible to int you will get false in return and the value of CHWTemp will be 0.
Just check that your getting the correct string from the text box. That's it.

Actually your code seems good to me. To be safe, I created this fiddle, in which I removed all sql related stuff, and replaced them with Console.WriteLine's.
using System;
public class Program
{
public static void Main()
{
int CHWTemp;
String inputStr = "17";
int.TryParse(inputStr,out CHWTemp);
if (CHWTemp >= 0 && CHWTemp <= 10)
{
Console.WriteLine("UPDATE Temperature SET Temp = 25 where id=12");
}
else if (CHWTemp >= 11 && CHWTemp <= 20)
{
Console.WriteLine("UPDATE Temperature SET Temp = 24 Where id=12");
}
else if (CHWTemp >= 21 && CHWTemp <= 40)
{
Console.WriteLine("UPDATE Temperature SET Temp = 23 where id=12");
}
else if (CHWTemp >= 41 && CHWTemp <= 60)
{
Console.WriteLine("UPDATE Temperature SET Temp = 22 where id=12");
}
else if (CHWTemp >= 61 && CHWTemp <= 80)
{
Console.WriteLine("UPDATE Temperature SET Temp = 21 where id=12");
}
else if (CHWTemp >= 81 && CHWTemp <= 100)
{
Console.WriteLine("UPDATE Temperature SET Temp = 20 where id=12");
}
}
}
As you can see, it works just fine. So, it is easy to say, parsing part works as expected. Therefore I think the point of failure looks like how you get the string from your text field.
So, my suggestion for you is to make sure that CHWV.Text returns a string representation of the number you enter.
Cheers,

Related

Wrong sum is calculated in c# when trying to generate a valid number

I'm trying to generate a random number in the range specified and then decide if its valid or not based on some simple arithmetic.
The sum (right after the DEBUG comment) is supposed to be, for example
222 222 222
121 212 121
(the 121 212 121 coefficients are added to each digit. if the product of the numbers is more than 9, then the sum of the digits of the product is recorded otherwise just the product is recorded (21 or 22 in this situation.)
using 222 222 222 as an example number the sum would be...
21 + 22 + 21 + 22 + 21 + 22 + 21 + 22 + 2*1 = 26 in this situation.
The above operations are supposed to be made to the random number generated above, from the specified range. The problem is that this sum is incorrect, when checking it by hand. Please help...
private void button1_Click(object sender, EventArgs e)
{
bool sinvalid = false;
Random randomsin = new Random();
//do until a valid sin is found
do
{
int rsin = randomsin.Next(100000000, 799999999); //get a new random sin
if (Validate(rsin.ToString()) == true) //if validate returns true
{
textBox1.Text = rsin.ToString(); //put the valid sin in the textbox
sinvalid = true; //set current sin to valid
}
} while (sinvalid == false);
}
bool Validate(String sin)
{
int sum = Cdig(sin[0], 1) + Cdig(sin[1], 2) + Cdig(sin[2], 1) + Cdig(sin[3], 2) + Cdig(sin[4], 1) + Cdig(sin[5], 2) + Cdig(sin[6], 1) + Cdig(sin[7], 2) + Cdig(sin[8], 1);
//DEBUG: show this sum, which is always calculated WRONG!~ WHY
label1.Text = sum.ToString();
if (sum % 10 == 0) //if sum is divisible by 10
{
return true;
}
else
{
return false;
}
}
int Cdig(int dig, int n )
{
int dm = dig * n;
if (dm > 9)
{
return 1 + (dm % 10);
}
else
{
return dm;
}
}
i had to convert the chars from sin[0], sin[1] ..etc. to integers.
int sum = Cdig(sin[0]-'0', 1) + Cdig(sin[1]-'0', 2) + Cdig(sin[2]-'0', 1) + Cdig(sin[3]-'0', 2) + Cdig(sin[4]-'0', 1) + Cdig(sin[5]-'0', 2) + Cdig(sin[6]-'0', 1) + Cdig(sin[7]-'0', 2) + Cdig(sin[8]-'0', 1);
strange but it works
Because you're summing char not int
You can use Char.GetNumericValue or you can getbytes from string and use unsafe code
var sum = 0;
var n = 1;
for (int i = 0; i < sin.Length; i++)
{
//int sum = Cdig
var dig = (int) Char.GetNumericValue(sin[i]);
sum += Cdig(dig, n);
if (n == 1)
n = 2;
if (n == 2)
n = 1;
}

How to count the output of the console

This is a Grading Marksheet code and I am looking for how the app may count how many times a string (e.g: Grade is A) has been repeated. Thank you!
This code sample is the part where I need to count the string from
if (p > 60 && p <= 80)
{
Console.WriteLine("Grade is A");
}
if (p > 80 && p <= 100)
{
Console.WriteLine("Grade is A++");
}
Console.ReadLine();
The idea would be to simply increase a integer variable for each time you print an A.
Example with your Code as Base:
// Use a Variable that you increase in value
// To see how many A were printed
int acount = 0;
// Is number bigger than 60 and smaller or equal to 80
if (p > 60 && p <= 80)
{
Console.WriteLine("Grade is A");
acount++;
}
// If the first statement is true the second can't be true anyway
// so use else if so it doesn't have to make usless checks
// Is number bigger than 80 and smaller or equal to 100
else if (p > 80 && p <= 100)
{
Console.WriteLine("Grade is A++");
acount++;
}
// Print out result
Console.WriteLine("A was printed: " + acount + " times");
Console.ReadLine();

Check to see if year is leap year

<script Language="c#" runat="server">
void Page_Load()
{
int currentYear = DateTime.Now.Year();
if (currentYear % 400 == 0) {
Message2.Text = ("This is a leap year");
}
else {
Message2.Text = ("This is not a leap year");
}
}
Currently I am getting a RunTime error. My goal is to test whether or not the current year, using DateTime.Now.Year() is a leap year or not. I think the issue is that I am not properly converting year to int? Please advise.
You can just use DateTime.IsLeapYear():
if (DateTime.IsLeapYear(year))
{
//do stuff
}
For those that come here for the rules:
According to wikipedia, these extra days occur in each year
which is an integer multiple of 4.
years that are evenly divisible by 100 are not leap years unless evenly divisible by 400.
So this results in this function:
// PRE: no year < 1 or > 9999
// POST: true if year is a leap year, or false if not.
public static bool IsLeapYear(int year)
{
if (year < 1 || year > 9999)
{
throw new ArgumentOutOfRangeException("year", Environment.GetResourceString("ArgumentOutOfRange_Year"));
}
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
And now I'm wondering what should happen if I use minus-years for BC - actually, the question is which calendar does even apply, especially prior to 1753 (for Britain) ? ;)
I use C# and this code for leap years.
cheers
if ((jahr % 4 == 0 && jahr % 400 == 0) || (jahr % 4 == 0 && !(jahr % 100 == 0)))
{
Console.WriteLine(jahr + " ist ein Schaltjahr");
}
else
Console.WriteLine(jahr + " ist kein Schaltjahr");
static int GeveDays()
{
int days;
if ((DateTime.Now).Year / 4 != 1 || (DateTime.Now).Year / 400 != 1) {
Console.WriteLine("it is a common year");
days = 365;
return days;
}
else if ((DateTime.Now).Year / 100 != 1) {
Console.WriteLine("it is a leap year");
days = 366;
return days;
}
else {
Console.WriteLine("it is a leap year");
days = 366;
return days;
}
}
int x= int.Parse(Console.ReadLine());
if((x%400==0)||(x%100==0)||(x%4==0))
{
Console.WriteLine(" \n\n\n The year is a leap year ...!");
}
else
{
Console.WriteLine("\n\n\n The year is not a leap year");
}

C# IF statement about a int check vs string in a DataTable

I have a DataTable where I have a column "Number" number is a string.
I would like to do
if (row["Number"].ToString() >= 1 && row["Number"].ToString() <= 100)
So if the column number is higher than 1 or lower than 100 do this.
Is there anyway to do this?
You can parse the string into an int by using int.TryParse:
int rowNumber;
if (!int.TryParse((string)row["Number"], out rowNumber)
{
// Not an int
}
if (rowNumber >= 1 && rowNumber <= 100)
{
// Do stuff
}

c# More elegant way of finding if number is in range

At the moment in one of my projects that I am working on, I need to check if a value falls between a number divisible by 12 and a number divisible by 12 + 5.
if (Number >= 0 && Number <= 5) {
value = 0;
} else if (Number >= 12 && Number <= 17) {
value = 12;
} else if (Number >= 24 && Number <= 29) {
value = 24;
}
// etc...
The code above works perfectly but I feel that it could be cut down. Does anyone have an alternative way of how to achieve what I am going for but more elegantly?
Use the % operator. (See http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx)
This should work as long as Number is positive; if it's not you'll have to look into how % works for negative numbers:
if (Number % 12 <= 5) {
value = 12*(int)(Number/12);
}

Categories