|
In your statement var Val is the same as int Val cause GetInt32() returns a int on success.
var Val = odbcDataReader[nColNumber]; is what you should use.
Val will be an object of type Object and you can check for null and do the casting later.
This is obviously for debugging purposes. Cause the cast and checking are not necessarily once you figure
out the problem.
All the best,
Dan
|
|
|
|
|
Hi Dan,
Using the indexer, casting to an int always works and casting to a string always fails (invalid cast).
So the behavour is predictable and my problem 'kind of' goes away. So for now I will use the indexer and not use the Get???? methods.
@PIEBALDconsult: You mentioned indexers earlier but I was not sure what you were getting at. Turns out you were on to something. Thanks.
|
|
|
|
|
Again you used nColNumber twice. You should have used nColNumber and nColName.
That was your problem. And yeah I don't use GetXXX either. But that wasn't what caused the exception.
You tried to get a string from an int DB value or a int form a varchar DB value courtesy
of using twice nColNumber.
All the best,
Dan
|
|
|
|
|
Well, personally, I never use the Get methods anyway; I just use the indexers.
__John_ wrote: odbcDataReader.IsDBNull(nColNumber);
Shouldn't you be testing the result?
|
|
|
|
|
What is the query that gets executed on the DB while doing a cmd.ExecuteReader()?
Maybe there is something missing/wrong there.
All the best,
Dan
|
|
|
|
|
Hi Moshu,
The command that I execute is as follows..
odbcSelectCommand.CommandText = @"SELECT name, iq FROM Table_People WHERE iq < 3 ORDER BY iq";
I get the 'name' and 'iq' but I am only concurned with 'iq' for now.
|
|
|
|
|
OK. Heres what I would do. Run that query in SQLManagementStudio and check the results.
Are there any? Or some with a field null or...
All the best,
Dan
|
|
|
|
|
Hi MDL=Moshu, thanks for looking at this.
The code now looks like this...
int nColName = odbcDataReader.GetOrdinal(@"name");
int nColNumber = odbcDataReader.GetOrdinal(@"iq");
while (odbcDataReader.Read())
{
if(odbcDataReader.IsDBNull(nColNumber))
{
continue;
}
int nNumber = odbcDataReader.GetInt32(nColNumber);
string strNumber = odbcDataReader.GetString(nColNumber);
}
No nulls are returned and the behaviour is the same ie. with the call to IsDBNull() GetString() fails, and without it, GetInt32() fails.
|
|
|
|
|
This is my final reply. First check my post below.
Second you're using nColNumber for both the name and the iq .
That's not right and I pointed it out on my first post to this thread.
If for some reason you want a string representation of the number/iq then
just use strNumber = nNumber.ToString();
Hope it helps. I'm out of CP for today. Going to program some beers now.
All the best,
Dan
|
|
|
|
|
Also since you are not using a Select * statement, I would drop the lines of code that get the ordinals.
You know them name is 0 and iq is 1 so in a nutshell:
while(dr.Read()){
string name = dr[0].ToString();
int iq = (int)dr[1];
}
All the best,
Dan
|
|
|
|
|
Thanks dan,
But I think my way is more robust and future proof.
ie. it will still work even with 'SELECT *'.
- John
|
|
|
|
|
True. Then use dr["name"] and dr["iq"] and it will be future proof.
All the best,
Dan
|
|
|
|
|
Hi MDL=>Moshu,
Using an indexer as you sagest (with column name) is fine if you only have a small number of fields, but if the number of fields is higher then I think you will take a performance hit.
|
|
|
|
|
__John_ wrote: my way is more robust
That doesn't sound very convincing as your code does not produce the expected results to begin with.
Have you already figured nColNumber is appearing too many times in your code? (courtesy MDL)
|
|
|
|
|
MDL=>Moshu wrote: string name = dr[0].ToString();
It's already a string, just cast it -- string name = (string) dr[0]; , no need to call a method.
|
|
|
|
|
True.
Is there a performance reason too, or just style?
Cause if there is one, I might need to change some lines of code. I've always happily used ToString() on
datareader where the element is a text/string.
All the best,
Dan
|
|
|
|
|
There must be at least a small overhead in calling the method.
|
|
|
|
|
Your code changed. When you test things you should change 1 item at a time, however, you have also changed order and type. In your last example, you are calling GetString and GetInt32 on nColNumber which refers to an integer, your code should read:
int nameColumn = odbcDataReader.GetOrdinal(@"name");
int idColumn = odbcDataReader.GetOrdinal(@"iq");
while (odbcDataReader.Read()){
odbcDataReader.IsDBNull(idColumn);
int iqValue= odbcDataReader.GetInt32(idColumn);
string nameValue = odbcDataReader.GetString(nameColumn );
}
I just typed it in the post window but you can see how different variable names make it easier to see.
|
|
|
|
|
Hi,
We are using VSS for our windows c# projects...
What is the best and easiest way to monitor the changeRequests with the deployment versions?
Currently, each developer is working on a changerequest i.e. add a print button to FormA, etc...
Should there be a link between the version of the vss and the changerequest?, etc?
Thanks
|
|
|
|
|
The general version control would be to use a Rationalclearcase tool or UCM.Are you looking at something other than version control tools?
|
|
|
|
|
|
For a efficient version control and tracking its better to use the version control tools(Rational clear case/UCM ) this will give us the right code deployed at any instance.We can even roll back the CR's or merge the code.
Dealing only with VS is little tricky..as i doubt whether we can rollback the changes if you dont require the CR?
-Manognya.K
_________________________
$ God gives what is best.Not what all you wish
|
|
|
|
|
Hi All
I have a website developed using C# Asp.Net Framework 4.0 and currently the site menus are not functioning on Mozilla Firefox, Opera & Safari browsers. On these browsers I can see the site but Menus are not showing. The site is working perfect on Internet Explorer but I want it to work the same on other Internet browsers, please help.
|
|
|
|
|
I do not think this is likely to be a C# issue.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
This may sound like a "blow you off" response, but I assure you that's not my intent: you are proposing the equivalent of blood transfusions between different species.
The answer, I think you'll find, will be found in implementing client-side UI using jQuery, and/or other libraries that extend JavaScript.
best, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
|
|
|
|