Join 150,399 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 971 people online right now. Registration is fast and FREE... Join Now!
//this is where I am attempting to assign the vehicleID variable to the //result of the getstring method vehicleID = reader.GetString(0); } }
catch (Exception exc) { lblException.Visible = true; lblException.Text = "There is a problem with your connection: " + exc.ToString(); } finally { conn.Close(); }
// get result from SELECT above and convert to int int vehicle = Convert.ToInt16(vehicleID); //this is where it hits an error - it says that vehicleID is not assigned to
I think I'm missing something small and frustrating...
Code looks fine, except for two things. First is your error, or probably warning. The message simply means that vehicleID may logically never have anything assigned to it. I'd prefer assigning null, string vehicleID = null; for the simple reason that explicitly tells us nothing was returned.
The other thing, you're only expecting one value? Use an if instead of a while to make that clear. Even better, with the example you have, use ExecuteScalar. e.g.
csharp
// set up a default value int vehicle = -1; try { com.Connection.Open(); vehicle = Convert.ToInt32(com.ExecuteScalar()); } catch (Exception exc) { lblException.Visible = true; lblException.Text = "There is a problem with your connection: " + exc.ToString(); } finally { com.Connection.Close(); } // if (vehicle == -1), something when horribly wrong.
Thanks baavgai. That does look a whole lot cleaner and more intuitive. I will definitely replace it with an ExecuteScalar(). Sometimes when I get an idea in my head its hard to see the other options. Just one thing though: Is it a better option to Convert to an Integer32 vehicle = Convert.ToInt32(com.ExecuteScalar()); as you've done here than converting to Int16 as I did?
EDIT: I just realized why I didn't use an ExecuteScalar() the first time. The command is a SELECT statement that returns a key value which is an integer. I don't think that an ExecuteScalar() will work here... or am I wrong?
This post has been edited by Footsie: 28 Feb, 2008 - 08:09 AM
Is it a better option to Convert to an Integer32 vehicle = Convert.ToInt32(com.ExecuteScalar()); as you've done here than converting to Int16 as I did?
The statement int is shorthand for System.Int32. So Int16 doesn't make much sense, unless you declare it in both places or are intentionally looking to loose precision. As a quick proof, try this:
csharp
int i = 5; Debug.WriteLine(i.GetType().FullName);
QUOTE(Footsie @ 28 Feb, 2008 - 10:20 AM)
I just realized why I didn't use an ExecuteScalar() the first time. The command is a SELECT statement that returns a key value which is an integer. I don't think that an ExecuteScalar() will work here... or am I wrong?
The ExecuteScalar returns a value of type "object". Technically, so does the reader, but it offers little convience functions that call Convert for you. e.g. reader.GetString(0); should be identical to Convert.ToString(reader.GetValue(0)); However, the methods like reader.GetInt32 will complain differently if the value doesn't match the datatype. Converting GetValue yourself is often the safer option, since the reader may have a difference idea of the data type it's getting from the data source.
If a null can bring down your application, then your code is very poorly written. Worse, if a empty string passes cleanly through, what subtle ways might if fail later?
Some place up the chain you might end up stating that null is an empty string, but that still forces you to deal with it, rather than safely allowing it to sneak by.
Of course, a null start value is just a preference. However, if you deal with passing many types of objects, handilng non initalized values consistently across all objects has advantages. See the NullObject pattern for an example of why the behavior is considered desirable. As of .NET 2.0, this pattern is implemented natively.
If you are only returning a single value from an SQL statement then ExecuteScalar is exactly what you need.
Ok great, I tried the ExecuteScalar and it worked well. I was under the impression that you could only return results of queries such as COUNT with an ExecuteScalar. Just to clarify - If I was returning a single value of say a string, which was a name such as "Allan" then an ExecuteScalar would also work here? Just as long as it is one name and not multiple? I thought that a string would count as multiple values...
QUOTE(baavgai Posted Today @ 07:09 PM )
The statement int is shorthand for System.Int32. So Int16 doesn't make much sense, unless you declare it in both places or are intentionally looking to loose precision.
I thought it was the other way around - int being short for Int16! That's why it's good to discuss these things and work out the kinks I suppose.
If I was returning a single value of say a string, which was a name such as "Allan" then an ExecuteScalar would also work here? Just as long as it is one name and not multiple? I thought that a string would count as multiple values...
That is correct, you can return any SQL Data type using the ExecuteScalar method, so long as it is only returning a value from one column in one row. Or it can be used with the aggregate functions like Count, Sum, Max...etc these functions will only return one value for a result.
If a null can bring down your application, then your code is very poorly written. Worse, if a empty string passes cleanly through, what subtle ways might if fail later?
Some place up the chain you might end up stating that null is an empty string, but that still forces you to deal with it, rather than safely allowing it to sneak by.
Of course, a null start value is just a preference. However, if you deal with passing many types of objects, handilng non initalized values consistently across all objects has advantages. See the NullObject pattern for an example of why the behavior is considered desirable. As of .NET 2.0, this pattern is implemented natively.
I completely agree with you on Null Pattern and all. But Strings are such a thing that developers forget to check them against null. We can't assign a blank string to every object in Java. So null has to be there. But at least for String we have a work around. And its not that i have initialized a String reference with null then I will be the only coder using that variable, others may be calling my functions to use the same reference and if they don't check Strings against null you know the user will keep on clicking that dumb submit button.
*edit Oh and by the way, have you carefully read that page on wikipedia?? It also says that avoid using null reference, instead implement Null Interface. Below is the quoted text from wiki:
QUOTE
Instead of using a null reference to convey absence of an object (for instance, a non-existent customer), you use an object which implements the expected interface, but whose method body is empty.
Hope i make some sense out of this non-sense
This post has been edited by bhandari: 28 Feb, 2008 - 10:46 PM