|
Do you have the following structure ?
while(sqlReader.Read())
{
}
|
|
|
|
|
HasRows doesn't tell you how many rows you haven't "Read() ", it only tells you if there are rows in the reader. Read advances you to the next row, starting from one before the first; the first time you call it you're placed at the first row.
To loop through all of the rows use this:
while (sqlReader.Read())
{
}
For Reference: MSDN[^]
|
|
|
|
|
I have the following
while (sqlReader.Read())
{
while(sqlReader["SOME FIELD"] == variable)
{
sqlReader.Read()
if (!sqlReader.HasRows)
{
blEnd = true;
break
}
if (blEnd){break}
}
}
Jude
|
|
|
|
|
What if the data is NULL in the database? Before you check sqlReader["SOME FIELD"] against your target value you should check it against DBNull.Value .
Also, this:
sqlReader.Read()
if (!sqlReader.HasRows)
has the same problem as before. HasRows doesn't care if you called Read or how many times it's called. It always returns the same thing. You shouldn't have more than one call to Read and you should always check it's return value.
What it looks like you're trying to do if go through all of the rows in the reader and do some work when the value of sqlReader["SOME FIELD"] equals variable . If you found variable then exit the loop.
If that's what you're doing then try this:
bool found = false;
while (sqlReader.Read() && !found)
{
if (sqlReader["SOME FIELD"] == DBNull.Value)
{
continue;
}
string dbVal = sqlReader["SOME FIELD"] as string;
if (dbVal != variable)
{
continue;
}
found = true;
}
Even if it's not what you're doing, note that there's only one Read , it's return value is checked and we're checking for DBNull.Value . dbVal should be the same type as whatever variable is, I just picked string as an example. Also note that <code> HasRows is not needed.
|
|
|
|
|
Thank you much for your reply.
What I am confused about in your example is the comment to 'continue to the next row'. How do I advance the cursor without calling Read()?
Jude
|
|
|
|
|
continue[^]
The continue; statement returns the code to the while() line which calls Read() , and that's how it advances to the next row
|
|
|
|
|
In fact you should use it in the other way:
DataTableReader dataTableReader = myDatabase.CreateDataReader();
if (dataTableReader.HasRows)
{
while (dataTableReader.Read())
{
Console.Out.Write(...);
}
}
Read will returns a boolean and does not through an exception.
Moreover it also should depend on your provider (MySQL, SQL Server...), the cursor method used behind may conduct to the problem.
You should read that you can use Read before HasRows but in the past I got some problems with some providers.
Hope it helps
|
|
|
|
|
DataTableReader dataTableReader = myDatabase.CreateDataReader();
if (dataTableReader.HasRows)
{
while (dataTableReader.Read())
{
//// write here... eg : messagebox.show("oUT pUT ");////////
}
}
|
|
|
|
|
Here is the exact error I am receiving:
System.InvalidOperationException: Invalid attempt to read when no data is present
But sqlReader.HasRows property is True.
Jude
|
|
|
|
|
|
What exactly do you want to get as output xml? can you please give an example?
|
|
|
|
|
Hi everybody,
I was looking a java code, and I would like to translate to C#, but I don't know if this feature exits in c#. The code is this:
public class Controller implements IController{
private State state;
private Speed speed;
private ProductHandler producthandler = new ProductHandler (){
@Override
public void changeState(State newState) {
state = newState;
}
@Override
public Speed getSpeed() {
return mds;
}
@Override
public void send(Packet pk) {
outputQueue.add(pk);
}
@Override
public void setSpeed(Speed newSpeed) {
speed = newSpeed;
}
};
ProductHandler is a class defined inside other class "dinamically". ProductHandler can access Controller attributes. Is this possible in C#? I have done it with events and so... but its very dirty. Is this annonymous classes, am i right? Is there something equivalent in .net 2.0?
Regards
modified on Friday, January 8, 2010 2:50 PM
|
|
|
|
|
C# has anonymous methods, but not anonymous classes. Maybe because you can use delegates, so there is no need for a whole class.. (according to the designers)
Anonymous methods can access the variables of the place they were declared in as well
|
|
|
|
|
I don't know Java, so I'm guessing a bit!
If you want to declare a new class nested inside another, you can and the nested class can access all members of the outer class.
If you want to extend an existing class, you can derive from it. Another option is to use extension methods, although they can only access the public members of the original class.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Thanks to both.
Nick, I have tried to do as you say, but I could't. Could you please paste a little example here?
Thanks in advance
|
|
|
|
|
You could simulate this using dynamic methods. Have a look around this[^] topic.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
ika2 wrote: Is there something equivalent in .net 2.0?
C# don't support anonymous classes. It has support for anonymous types, but it will not help you in this case. I guess what you need can be achieved using nested classes. Nested class will have access to all members of the class where it is written. Here is an example.
public class Controller : IController
{
private State state;
private Speed speed;
public class ProductHandler
{
public void ChangeState(State newState)
{
}
}
}
Best wishes,
Navaneeth
|
|
|
|
|
if i compile my wpf project in xp
and execute it on vista.
will it be executing normally.
i had an experience it from windows form application. it was not ruing on vista. i faced color combination problem.
|
|
|
|
|
hotthoughtguy wrote: if i compile my wpf project in xp
and execute it on vista.
will it be executing normally.
Yes, given that the appropriate version of the .Net framework is installed.
hotthoughtguy wrote: i had an experience it from windows form application. it was not ruing on vista. i faced color combination problem.
If you're talking about color combination problems in the user interface, they are the fault of the programmer, not the operating system.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
but the same win form project is executing normal in xp. if programming fault is there. so it should not execute normally in xp as well as vista
|
|
|
|
|
There shouldn't be any problems given that the same framework is installed as was said before. The best way to find out is to try it. if you face problems and you cant figure out solutions, post more questions.
|
|
|
|
|
|
YOu mean Compress method used by NTFS partiton?
This kind is compressed by OS. So it automatcly gets decompressed befpre zse and you do not need to handle it. If there was a need to handle it, then there would be a lot of broken apps
|
|
|
|
|
So I've created this application....
I realized that when it installs, all it installs is just a Shortcut to an Application Manifest.
The problem is, that the server the installer is on that it checks for updates etc.... if it ever goes down then that makes me thing that the shortcut wouldn't work thus the users can't use the program. How do i make it so that my C# program actually Installs the program itself on the users PC and not just a shortcut?
|
|
|
|
|
Are you using Visual studio installer? If yes, take a look at this article[^]
|
|
|
|