Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have an sqlite database, and a table, in which i have a column PIN as a TEXT, and it's empty.
when i'm getting it from the database, and trying to convert into a string it brings me an exception, When casting a number the value must be less then infinity...what is the reason?
In databases like sqlite, empty strings and null are two different things. It looks like you may be trying to cast your PIN to an int, or some other integer type. If the PIN field is null or DBNull, this cast will fail, giving the exception you listed. To further complicate things, null != DBNull.Value, which is what your database query is likely returning.
To check for this, you need to check the PIN field against DBNull.Value and cast only after you've checked.
See: DBNull Class
See: A similar SO question
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I want to check if my Listentry = null but I don't know how.
Note:
ID is a Integer
Here is my Code
if(MeineGaeste[i].ID !=null)
{
i++;
}
else
{
MeinGast.ID = i++;
test = false;
}
As stated, an int cannot be null. If a value is not set to it, then the default value is zero. You can do a check for 0 if that is what you think it is being set to...
Otherwise if you are keen on avoiding nullable integers, you would have to turn it into a string and then perform the check.
String.IsNullOrEmpty(ID.ToString());
Which returns a boolean so you can stick it into an if statement.
An int cannot be null. On the other hand a nullable int, int?, can be. That being said the check that you want to do is meaningless.
Changing the type of ID from int to int?, the check you do has a meaning and the compiler wouldn't complain about it.
you can use this ID.HasValue it return true when there is a value and false if null
As a normal int type is a value type, it can not be null.
You can use the nullable type int? for the property ID which is basically a struct, that allows null or any int value. See MSDN or this question or this question for additional documentation.
If it is not possible to change the type of the ID property, a workaround might be to choose an integer as marker value which is guaranteed not appear in your model (for example Int32.MinValue, as most generated IDs seem to be positive integers). Don't do this if you don't have to, as this will most likely lead to errors. I would a least recommend to throw an exception if you serialize models with that marker value.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a SQL command in C# that gets the manager field from a database like such:
SELECT manager FROM table WHERE number = '123456'
I then add it to a list like such:
c.Manager = Convert.ToString(sdr["manager"]);
I eventually add the result to a table. My problem is that in the database sometimes there is no entry for the number does not exist or the manager is just a string of whitespace.
How could assign the value " - " to c.Manager in these cases?
You could test for the value to see whether it's null or whitespace.
There's a handy built-in method in the String class:
var manager = Convert.ToString(sdr["manager"]);
c.Manager = string.IsNullOrWhiteSpace(manager) ? "-" : manager;
c.Manager = Convert.ToString(sdr["manager"])==""?"-":Convert.ToString(sdr["manager"]);
You could do a single line if statement
c.Manager = (Convert.ToString(sdr[#manager#]) == "") ? "-" : Convert.ToString(sdr["manager"]);
How about you tell your SQL script to return '-' instead of a whitespace or Null?
SELECT
CASE WHEN manager IS NULL
OR LTRIM(RTRIM(manager)) = ''
THEN '-'
ELSE manager
END AS manager
FROM table
WHERE number = '123456'
Benefit - Avoids data manipulation in C# code. Also keeps further changes easier and you can now simply assign the value to c .Manager.
NOTE - I firmly believe that if any data manipulation like the one in your case needs to be done, should be done while generating the resultset for better maintenance sake at least.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am working on a little project, that at a certain point need to convert a string to int.
I tried the following things:
//first try
value1 = int.TryParse(value[0].tostring(), out i)
//second
value1 = Convert.ToInt32(value[0].tostring())
//third
value1 = int.Parse(value[0].tostring())
I even wrote my own conversion method because I was at a loss.
The values I am trying to convert are queried from a MySQL database.
Thank for your help
EDIT:
I know tryparse should have 2 params.
And the error iam getting is a formatexception
Input string was not in a correct format.
I got that on all the tries.
the value in my test case is 2500
Keep in mind that that number is received from a db
I tried the above snippets while using a hard coded value. And that works fine.
EDIT 2:
//http://imgur.com/NSyg2rJ
One:
The signature of int.TryParse() is incorrect:
int result = 0;
bool parsedSuccessfully = Int32.TryParse(value[0].ToString(), out result);
//If successful, result will hold the value.
Two:
The signature of Convert.ToInt32() is correct, minus the case of the .ToString() method. This is failing because value or value[0] is null, or it cannot convert something like the string "NotAnInt" to an int.
Three:
The signature of int.Parse() is correct, but is failing for the same reasons as two.
Try this -
int value1;
if (Int.TryParse(value[0].ToString(), out value1))
{
//conversion successful
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
string banana = "banana apple";
banana.Replace("apple", "pie");
If I want to replace apple with pie, can I do it like that, or do I need to use the following?
if(banana.Contains("apple"))
banana.Replace("apple", "pie");
You just have to read msdn: ( or try it out yourself )
Return Value Type: System.String A string that is equivalent to the
current string except that all instances of oldValue are replaced with
newValue. If oldValue is not found in the current instance, the method
returns the current instance unchanged.
Side-note: since strings are immutable(you cannot change the instance) you have to reassign a new string if you want to change the old:
banana = banana.Replace("apple", "pie");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I cannot use Except keywords due to version problem. Anyone here know. How can i select all rows except not those rows which contain specific text.select all rows except those which contain specific text.I cannot use Except Keyword due to version problem in MS S.Q.L
select * from your_table
where some_column not like '%specific text%'
Select * from Table1
where EmpPU NOT Like '%CSE%'
AND EmpPU NOT Like '%ECE%'
AND EmpPU NOT Like '%EEE%'