|
Hi everyone. I'm new here and new to using C# coding as well so any help would be greatly apprectiated! I am trying to build my program but this message keeps popping up: Operator '||' cannot be applied to operands of type 'bool' and 'string'
I have tried everything that I know. Please help! The code is below:
using System;
namespace Program13
{
class StateAbbreviations
{
public static void Main( )
{
string stateabb;
Console.WriteLine("Enter all of the following state abbreviations: NC, AL, GA, FL, OH");
stateabb = Console.ReadLine();
Console.WriteLine("\n");
if (stateabb == "NC" || "nc")
Console.WriteLine("North Carolina");
else if (stateabb == "AL" || "al")
Console.WriteLine("Alabama");
else if (stateabb == "GA" || "ga")
Console.WriteLine("Georgia");
else if (stateabb == "FL" || "fl")
Console.WriteLine("Florida");
else if (stateabb == "OH" || "oh")
Console.WriteLine("Ohio");
else Console.WriteLine("Not one of the selected states");
}
}
}
|
|
|
|
|
Hi,
try if (stateabb == "NC" || stateabb == "nc")
I advise you buy and study a tutorial book on C#, any book will do you good.
|
|
|
|
|
Oh my gosh! Thank you so much. Worked like a charm. I forgot about that haha.
|
|
|
|
|
Why don't you actually read the error message and check against your code?
it said "Operator '||' cannot be applied to operands of type 'bool' and 'string'" which is what you had, an || operator in between a bool and a string.
|
|
|
|
|
I know. Like I said, I am very VERY new to this so I am getting used to all these new terms. I am kinda getting the hang of it though.
|
|
|
|
|
So 'Nc ' isn't a valid entry, is it (of course doesn't make sense discriminating such a bad way 'Carolina'...)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
May I suggest you use a HashSet?
System.Collections.Generic.HashSet<string> StateAbbs =
new System.Collections.Generic.HashSet<string>() ;
StateAbbs.Add ( "NC" ) ;
...
if ( StateAbbs.Contains ( stateabb ) ) ...
Or something like that, perhaps with a case-insensitive comparer.
|
|
|
|
|
Hi everyBody,
I need the code for sending SMS from PC while the mobile set is connected to PC through USB port.
I have found the code for Com port, but I need the code for USB port.
Thanks for your help.
My ID: invisible_eagle@yahoo.com
|
|
|
|
|
Yep...and i need the code to be rich. Anyone interested to share it for free and let me live as a happy and lazy parasite ?
|
|
|
|
|
I am having a strange issue with reading from Excel with C#
Here is my code:
string excelConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", txtExcel.Text);
OleDbConnection con = new OleDbConnection(excelConnectionString);
con.Open();
OleDbDataAdapter da = new OleDbDataAdapter("select * from ["+ txtFY.Text +"$]", con);
fy.Text = txtFY.Text;
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
dataGridView1.DataSource = dt;
dataGridView1.Refresh();
Now it works.. its just some columns that have information in the "actual" excel sheet doesn't appear in the data grid view..
Example:
Here is a column (Serial Column):
136WFW7552
136WFW7556
136WFW7494
136WFW7553
136WFW7492
70101135
71000640
70202250
70101234
70101230
Now when the numbers go from 136WFW7492 to 70101135, it doesn't put the 70101135 numbers in the datagridview... I've removed formatting from the workboot and everything.. I'm confused
|
|
|
|
|
What data/columns are in the raw DataTable? Is it what is to be expected?
Try putting HDR=Yes; in your connections string. I can't remember off-hand what the default is.
Edit:
This article pretty much sums it up. Reading and Writing Excel using OLEDB[^]
hmmm pie
modified on Friday, April 24, 2009 5:18 AM
|
|
|
|
|
I've tried that document and it doesn't seem to work.. Any other ideas?
|
|
|
|
|
There's always the alternative way of Opening the file itself. Have a look at this example[^]
Also have a look at this: Opening and Navigating Excel with C#[^]
It's not ideal but it's a work around.
My failometer is detecting vast quantities of FAIL!
|
|
|
|
|
Im not sure if it will work or not but you may be able to select the columns using GetOleDbSchemaTable().
Then use those columns in the select query,
private string[] _GetColumnsFromWorkseet(string sheetname, out string errorMessage)
{
try
{
List<string> ls = new List<string>();
DataTable dtCols = _oleDbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new Object[] { null, null, sheetname, null });
foreach (DataRow dr in dtCols.Select(string.Empty, "ORDINAL_POSITION"))
ls.Add(dr["COLUMN_NAME"].ToString());
errorMessage = string.Empty;
return ls.ToArray();
}
catch (Exception ex)
{
errorMessage = ex.Message;
return null;
}
}
My failometer is detecting vast quantities of FAIL!
modified on Friday, April 24, 2009 10:20 PM
|
|
|
|
|
I actually got it to work on that article. I changed the reg key, turned HDR to YES, but what I had to do was put in IMEX=1 which I don't think was in that document.
|
|
|
|
|
Its aknown problem using oledb to read from Excel. It decides the column data type from the first (I think) 8 rows, and any subsequent data that does not match the data type is ignored.
I can see the sense if the column looks like a number and later you get text, but not the other way round. Anyway, thats how it works. It has caught me out before.
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
I tried modifying from that article and it doesn't work.. any other ideas?
|
|
|
|
|
I just opened excel from within my app, saved the data as csv and rea dit from there.
Bob
Ashfield Consultants Ltd
Proud to be a 2009 Code Project MVP
|
|
|
|
|
hui guyz i am working on an ftp client which is working quite properly but have an issue it only uploads and downloads files one by one, so plz help.
|
|
|
|
|
The answer is obvious - you need to select multiple files and upload them in a loop. You can't FTP more than one file at once.
Christian Graus
Driven to the arms of OSX by Vista.
"I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )
|
|
|
|
|
I am trying to launch a Google search for a specific string from with my application, and this is all working perfectly, until the search string contains quotes. If there are quotes in the search string, then all the spaces inside the string are removed.
In the debugger, the string looks like this:
http://www.google.com/search?hl=en&q="Just For Laughs Comedy Tour"
But when Google gets it, the URL looks like this:
http://www.google.com/search?hl=en&q="JustForLaughsComedyTour"
If there are no quotes around the search string, then it works perfectly. Running it from the command line doesn't work at all unless I remove the quotes around the search string and place them around the entire command.
The code I am using is here:
System.Diagnostics.ProcessStartInfo pInfo = new System.Diagnostics.ProcessStartInfo(strCmd, strArgs);<br />
pInfo.Verb = "open";<br />
pInfo.ErrorDialog = true;<br />
pInfo.WorkingDirectory = strDir;<br />
System.Diagnostics.Process.Start(pInfo);
I realize that I could probably UrlEncode the string or something, but I am using a generic application launcher thingie that doesn't know anything about the application it is launching - it just calls start on whatever string you pass it. Actually, it is a version of the Visual Studio "External Tools" manager that I wrote[^] as a .NET control, so I don't want it to have to treat URLs differently to standard commands, and I don't want the command to be "IExplore.exe" either. I want it to launch the users default browser based on a URL that I give it.
Does anyone know how to workaround this quoted search string weirdness, without having to resort to a special case to fix it?
Thanks!
|
|
|
|
|
|
fly904 wrote: Replace ' ' with '+'.
I can't, because the code that launches the command doesn't know what sort of command it is. In other words, as far as the launcher knows, these two strings are equivalent and should be treated exactly the same:
http://www.google.co.uk/search?q="Just For Laughs Comedy Tour"
notepad "Just For Laughs Comedy Tour"
Interestingly, if you paste the following command into the Run box in Windows XP it doesn't work either, but if you paste it directly into your browser it works just fine. This leads me to think there's something strange going on inside Windows somwehere.
http://www.google.co.uk/search?q="Just For Laughs Comedy Tour"
And just for laughs, try these too:
firefox http://www.google.com/search?hl=en&q="Just For Laughs Comedy Tour"
iexplore http://www.google.com/search?hl=en&q="Just For Laughs Comedy Tour"
They both work as expected.
|
|
|
|
|
I see your point now after testing it.
The only way I found was to 'double escape' the speech marks.
I just tried this and it worked perfectly after opening in my default browser (firefox).
string searchString = "\\\"Just For Laughs Comedy Tour\\\"";
System.Diagnostics.ProcessStartInfo pInfo = new System.Diagnostics.ProcessStartInfo("http://www.google.co.uk/search?q=" +
searchString.Replace(" ", "+"));
pInfo.Verb = "open";
pInfo.ErrorDialog = true;
System.Diagnostics.Process.Start(pInfo);
There's a part of me which hate's seeing spaces in URL's.
hmmm pie
|
|
|
|
|
I have a webpage displaying some information based on the querystring.
i.e., http://www.mywebsite.com/userdetails.aspx?userid=15510
The issue is, i want to access the information of the user while giving the url as http://www.mywebsite.com/15510
I don't know how to do that.Can anyone suggest how to achieve it?
TIA.
|
|
|
|