|
I'm suspicious that you're missing the code inside reader.Read(). I have the feeling that the problem you are looking at lies in that block. As a test, replace your reader.Read line with this
int count = 0;
while (reader.Read())
{
Debug.WriteLine("Count is {0}", ++count); Then, when you are debugging your app, take a look at what appears in the output window in Visual Studio.
|
|
|
|
|
One of the line in using section is reading the first record. In while loop, it is reading from second record. I removed the below if condition and it worked fine. Thank you all for your replies. Sorry for not posting that line, as I thought that line is handling only exception.
if (!reader.Read())
throw new ApplicationException("MISSING Transaction Returned By Financial Institution. Transaction was not found in the database.");
while (reader.Read())
{}
|
|
|
|
|
At least the mystery has been solved. The effect you were after here could have been achieved by using the following instead:
if (!reader.HasRows)
{
throw new ApplicationException("MISSING Transaction Returned By Financial Institution. Transaction was not found in the database.");
}
while (reader.Read())
{
} For future reference, the act of calling Read automatically moves the record to the next available record (if one's present) when it completes. It implements a firehose cursor, whereby data is effectively squirted at you.
|
|
|
|
|
Hi,
New to coding. Trying to teach myself. I am wanting to test an input against a name and if it is true then proceed. If it is not true display something different. Here is what I wrote along with the error. Obviously it's wrong. Any help would be appreciated. Thanks
Console.WriteLine(name + " What is her name?");
String herName = Console.ReadLine();
if (herName == ashley)
{
do this stuff...
}
Error 1 The name 'ashley' does not exist in the current context
modified 1-Sep-13 23:52pm.
|
|
|
|
|
You're comparing against a string literal, but what you wrote is comparing against the contents of a variable called ashley , which you never defined. That's why you're getting the error message.
If you're going to specify a string literal, the string must be enclosed in double-quotes:
if (herName == "ashley")
But, this poses a bit of a problem. The comparison is case sensitive. "Ashley" does not equal "ashley". You need something closer to this to fix that:
if (string.Compare(herName, "ashley", StringCompare.CurrentCultureIgnoreCase))
If the Compare method returns 0, the strings are equal according to the rules of the current culture and ignoring the case of any letters.
You REALLY need to pickup a beginners book on C# and work through it. Trying to learn a language through nothing but trial and error and forum posts will only slow you down and frustrate you.
|
|
|
|
|
Ok just going with the first example you gave me:
Console.WriteLine(name + " What is her name?");
String herName = Console.ReadLine();
if(herName == "ashley")
{
do this stuff..
}
Error 1 Use of unassigned local variable 'ashley'
{
I used the 2nd method as well and still get errors. The variable Ashley doesn't com into play until user input so I don't understand why I am getting the error.
|
|
|
|
|
Go back and read what I said again. Somewhere you have the string ashley without double-quotes around it.
|
|
|
|
|
I thought when the user input ashley here
String herName = Console.ReadLine();
The string with the name ashley would be created at that time. Anyhow I'm just not getting it. The code I have written works until I put an if statement into it
FYI I do not have the word Ashley anywhere else in the code. If I remove the if statement, the program works fine.
modified 2-Sep-13 1:02am.
|
|
|
|
|
I strongly agree with Dave's feedback that you need to get a good basic book on .NET, and go over the fundamentals: you are "spinning your wheels here."
Try this free book from the renowned educator Charles Petzold, ".NET Book Zero:" [^]. From that site you can download either a .pdf file, or an .xps version of the book, and a separate .zip file is available there for download that contains all the code examples in the book.
good luck, Bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
No, no, it just doesn't work like that!
Forget computers for a moment, and suppose you are on the phone, talking to a company. The lady at the other end asks you a question "What is your name?"
You reply "Skeeterz".
Does that move you physically into her company, or automatically create a file in her computer?
Of course not! That would be silly...
What it does do is transfer a small amount of information from one place (your home) to another (her office) - and she will make a note of it by jotting it on a piece of paper, perhaps, or typing it into her computer to see if there is any information on you allready.
The computer program you wrote does the same thing: It asks "What is your name?" and the user replies "ashley" - that transfers the information from the user space into the program space, and stores it in memory, putting a "reminder" for what her name is into the variable "herName". In future, whenever you want to use the users name, you just use the variable "herName":
Console.WriteLine("Hello " + herName + " and welcome!"); Or
UserDetails herInfo = LookupInTheDatabase(herName);
This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre.
Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.
|
|
|
|
|
Quote: Does that move you physically into her company, or automatically create a file in her computer?
Of course not! That would be silly...
Hahaha that would be awesome if person can physically move... kind of Mass transfer of human being!!! You will see in sci fi movies.
Thanks
-Amit Gajjar
|
|
|
|
|
Um. Might have side effects[^]
This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre.
Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.
|
|
|
|
|
skeeterz71 wrote: The string with the name ashley would be created at that time
Ok, think about this. You just said that if the user types a string into the Console or into a TextBox that a variable with the same name would be created. That's absurd for the following reason: What if the user typed:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis cursus justo arcu, quis euismod sapien blandit malesuada. Nam nisl augue, ornare id ipsum a, tristique feugiat justo. Donec sed eros id metus aliquam egestas. Donec fermentum condimentum mollis. Donec at auctor velit, non elementum purus. Maecenas congue, turpis non rutrum feugiat, magna ligula iaculis libero, eget aliquam erat magna at erat. Proin at mauris scelerisque, ultrices ipsum ac, mollis magna.
?? You really think a variable with that name is going to be created??
Seriously, you REALLY need to pickup a beginners book on C#. You will only serve to frustrate yourself if you don't properly learn the fundamentals.
|
|
|
|
|
Check to make sure the user entered anything by checking if the string is whitespace or null.
Checking if it is null is the most important.
Console.WriteLine(name + " What is her name?");
String herName = Console.ReadLine();
if(!String.IsWhiteSpaceOrNull(herName))
{
if(herName == "ashley")
{
do this stuff..
}
}
|
|
|
|
|
Hi, use this
if (herName == "ashley") instead of
if (herName == ashley)
This will resolve your error.
|
|
|
|
|
Reason for my vote of one: Three hours after Dave had posted this solution, with much better detail as to why to do it, you post exactly the same thing? How exactly is that supposed to help the OP?
This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre.
Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.
|
|
|
|
|
For someone who posts in Q&A so much, I'm shocked at your shock
|
|
|
|
|
Thank you! that worked. I really appreciate your help. I got a lot of suggestions here and yours is the only one that worked.
|
|
|
|
|
As other suggested, You have problem with the if condition. First you need to learn about the difference between variable and it's value. In the given if condition you have herName as variable of type string. Now you need to compare string with string only. That could be possible either by comparing string variable like,
string storedName = "ashley";
if(herName == storedName)
Or by directly comparing string with the variable like,
if(herName == "ashley")
Before go for the solution oriented learning just read one of the beginner book or online materials(MSDN) to understand concepts.
Thanks
-Amit Gajjar
|
|
|
|
|
Hi There !
in myTable I have 4 fields , MyId,FName,Lname,Year,Code
I want to Filter "Code" when the "Year" Field is equel to TextForm Like 2005 or --> Year=2005.
try
{
OleDbConnection ocn = new OleDbConnection(ConnectionString);
OleDbDataAdapter oda = new OleDbDataAdapter(@"SELECT MyId,FName,Lname,Year=<a href="/Members/P1">@p1</a>,Code,FROM myTable WHERE Code=<a href="/Members/P2">@p2</a>", ocn);
oda.SelectCommand.Parameters.Clear();
oda.SelectCommand.Parameters.AddWithValue("<a href="/Members/P1">@p1</a>",txtYear.Text);
oda.SelectCommand.Parameters.AddWithValue("<a href="/Members/P2">@p2</a>",txtCode.Text);
DataTable dt = new DataTable();
dt.Clear();
oda.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("This Code Used Before");
txtCode.Focus();
}
else
{
...
Thanks a Lot
modified 1-Sep-13 4:26am.
|
|
|
|
|
Very difficult to read with the corrupted formatting.
I think the following may be what you want
Select Count(*) from MyTable where Year = @P1 or Code = @P2
Use executescalar and box the return as an int and test that value
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Thanks
Excuse me for bad to Read :
a simple instance is Here :
I want to Edit my Records , and Prevent the Duplicate Records e.g. :
----------------------------------
YearReg RegNo
1950 112
1950 102
1951 110
1950 114
-----------------------------------
For example in The All Records that Year is 1950 , RegNo shoulbe Unique and if Year=1951 , RegNo also unique and etc.
heres my code For EditButton Click :
...
string strQ = @"SELECT COUNT(RegNo) FROM tblRcvLet WHERE YearReg=@s1"
OleDbDataAdapter oda_Edited = new OleDbDataAdapter(strQ, ocn);
oda_Edited.SelectCommand.Parameters.Clear();
oda_Edited.SelectCommand.Parameters.AddWithValue(" @s1", txtYearReg.Text);
DataTable dt_Edited = new DataTable();
dt_Edited.Clear();
oda_Edited.Fill(dt_Edited);
if (dt_Edited.Rows.Count > 0)
{
MessageBox.Show("Already Exists!! ");
txtRegNo.Focus();
}
else
{
OleDbCommand ocmd_Edited02 = new OleDbCommand();
ocmd_Edited02.CommandText = @"UPDATE ....
But I can Not Edit(Update) Records ...
Thanks a Lot
|
|
|
|
|
You can use datatable select option
string sql ="string strCondition = string.Format("Year = '{0}'", 2005);
DataRow[] row = dt.Select(strCondition);
|
|
|
|
|
I learn or relearn something every day - my excuse, I use collections these days.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|