473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Syntax Problem with If statement

I am trying to write a simple ASP.net/C# page which allows users to select
some values and produce a report based on a SQL query.

I have a self posting dropdown form which allows users to select the type of
report to generate:

Select the type of report to display:
<form runat="server">
<asp:DropDownList AutoPostBack="true" ID="report" runat="server">
<asp:listitem>Report Type 1</asp:listitem>
<asp:listitem>Report Type 2</asp:listitem>
<asp:listitem>Report Type 3</asp:listitem>
<asp:listitem>Report Type 4</asp:listitem>
<asp:listitem>Report Type 5</asp:listitem>
<asp:listitem>Report Type 6</asp:listitem>
</asp:DropDownList>
</form>

I am trying to write some conditional logic which determines which SQL query
will be run:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") {
string sql_query = "SQL QUERY";
}

// RUN THE QUERY

}
</script>

Unfortunately I keep getting the following error: Compiler Error Message:
CS1002: ; expected on the following line:

if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") {

I am sure this is a problem with my syntax but I'm just having problems
determining the correct syntax to use. Could anyone offer any pointers or
references to documentation?

Thanks in Advance,
Brad

Apr 17 '06 #1
12 1662

"Brad Baker" <br**@nospam.nospam> wrote in message
news:uv**************@TK2MSFTNGP05.phx.gbl...
I am trying to write a simple ASP.net/C# page which allows users to select
some values and produce a report based on a SQL query.

I have a self posting dropdown form which allows users to select the type
of report to generate:

Select the type of report to display:
<form runat="server">
<asp:DropDownList AutoPostBack="true" ID="report" runat="server">
<asp:listitem>Report Type 1</asp:listitem>
<asp:listitem>Report Type 2</asp:listitem>
<asp:listitem>Report Type 3</asp:listitem>
<asp:listitem>Report Type 4</asp:listitem>
<asp:listitem>Report Type 5</asp:listitem>
<asp:listitem>Report Type 6</asp:listitem>
</asp:DropDownList>
</form>

I am trying to write some conditional logic which determines which SQL
query will be run:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") {
string sql_query = "SQL QUERY";
}

// RUN THE QUERY

}
</script>

Unfortunately I keep getting the following error: Compiler Error Message:
CS1002: ; expected on the following line:

if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") {

I am sure this is a problem with my syntax but I'm just having problems
determining the correct syntax to use. Could anyone offer any pointers or
references to documentation?

Thanks in Advance,
Brad


There is no OR in C#. Try || instead...

if (report.SelectedItem.Text == null || report.SelectedItem.Text == "Report
Type 1") {

HTH :)

Mythran

Apr 17 '06 #2
use || instead of OR

-- bruce (sqlwork.com)

"Brad Baker" <br**@nospam.nospam> wrote in message
news:uv**************@TK2MSFTNGP05.phx.gbl...
I am trying to write a simple ASP.net/C# page which allows users to select
some values and produce a report based on a SQL query.

I have a self posting dropdown form which allows users to select the type
of report to generate:

Select the type of report to display:
<form runat="server">
<asp:DropDownList AutoPostBack="true" ID="report" runat="server">
<asp:listitem>Report Type 1</asp:listitem>
<asp:listitem>Report Type 2</asp:listitem>
<asp:listitem>Report Type 3</asp:listitem>
<asp:listitem>Report Type 4</asp:listitem>
<asp:listitem>Report Type 5</asp:listitem>
<asp:listitem>Report Type 6</asp:listitem>
</asp:DropDownList>
</form>

I am trying to write some conditional logic which determines which SQL
query will be run:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") {
string sql_query = "SQL QUERY";
}

// RUN THE QUERY

}
</script>

Unfortunately I keep getting the following error: Compiler Error Message:
CS1002: ; expected on the following line:

if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") {

I am sure this is a problem with my syntax but I'm just having problems
determining the correct syntax to use. Could anyone offer any pointers or
references to documentation?

Thanks in Advance,
Brad

Apr 17 '06 #3
Hi!

Check your syntax again:

if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1")

This line **SHOULD** read:

if ( (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") )

Note the addition of the enclosing parenthesis ...

-- Jake
--
Jacob W Anderson
---
http://www.beyond-ordinary.com
http://www.accessquery.com
---
If you think it''s expensive to hire a professional to do the job, wait
until you hire an amateur.

"Brad Baker" wrote:
I am trying to write a simple ASP.net/C# page which allows users to select
some values and produce a report based on a SQL query.

I have a self posting dropdown form which allows users to select the type of
report to generate:

Select the type of report to display:
<form runat="server">
<asp:DropDownList AutoPostBack="true" ID="report" runat="server">
<asp:listitem>Report Type 1</asp:listitem>
<asp:listitem>Report Type 2</asp:listitem>
<asp:listitem>Report Type 3</asp:listitem>
<asp:listitem>Report Type 4</asp:listitem>
<asp:listitem>Report Type 5</asp:listitem>
<asp:listitem>Report Type 6</asp:listitem>
</asp:DropDownList>
</form>

I am trying to write some conditional logic which determines which SQL query
will be run:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") {
string sql_query = "SQL QUERY";
}

// RUN THE QUERY

}
</script>

Unfortunately I keep getting the following error: Compiler Error Message:
CS1002: ; expected on the following line:

if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") {

I am sure this is a problem with my syntax but I'm just having problems
determining the correct syntax to use. Could anyone offer any pointers or
references to documentation?

Thanks in Advance,
Brad

Apr 17 '06 #4
Mythran -

Thanks for the tip :) That partially solved part of my problem. I'm no
longer getting an error on my conditional statement itself but it also seems
that my conditional statement isn't being met.

Looking at the code again, I now have:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
if (report.SelectedItem.Text == null || report.SelectedItem.Text ==
"Report Type 1") {
string sql_query = "SQL QUERY";
}

Response.Write(report.SelectedItem.Text);

// RUN THE QUERY

}
</script>
When I run the code above, Response.Write(report.SelectedItem.Text); prints
"Report Type 1" yet I am getting an error indicating the sql_query string
isn't getting set:

Compiler Error Message: CS0103: The name 'sql_query' does not exist in the
current context
Source Error: Line 31: SqlDataAdapter results_query = new
SqlDataAdapter(sql_query, objConn);

How frustrating! :) Could this be a problem with the scope of the sql_query
variable? Or do I have another syntax error?

Thanks Again,
Brad

"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:ei**************@TK2MSFTNGP03.phx.gbl...

"Brad Baker" <br**@nospam.nospam> wrote in message
news:uv**************@TK2MSFTNGP05.phx.gbl...
I am trying to write a simple ASP.net/C# page which allows users to select
some values and produce a report based on a SQL query.

I have a self posting dropdown form which allows users to select the type
of report to generate:

Select the type of report to display:
<form runat="server">
<asp:DropDownList AutoPostBack="true" ID="report" runat="server">
<asp:listitem>Report Type 1</asp:listitem>
<asp:listitem>Report Type 2</asp:listitem>
<asp:listitem>Report Type 3</asp:listitem>
<asp:listitem>Report Type 4</asp:listitem>
<asp:listitem>Report Type 5</asp:listitem>
<asp:listitem>Report Type 6</asp:listitem>
</asp:DropDownList>
</form>

I am trying to write some conditional logic which determines which SQL
query will be run:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") {
string sql_query = "SQL QUERY";
}

// RUN THE QUERY

}
</script>

Unfortunately I keep getting the following error: Compiler Error Message:
CS1002: ; expected on the following line:

if (report.SelectedItem.Text == null) OR (report.SelectedItem.Text ==
"Report Type 1") {

I am sure this is a problem with my syntax but I'm just having problems
determining the correct syntax to use. Could anyone offer any pointers or
references to documentation?

Thanks in Advance,
Brad


There is no OR in C#. Try || instead...

if (report.SelectedItem.Text == null || report.SelectedItem.Text ==
"Report Type 1") {

HTH :)

Mythran

Apr 17 '06 #5
On Mon, 17 Apr 2006 13:14:07 -0400, Brad Baker wrote:
Looking at the code again, I now have:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
if (report.SelectedItem.Text == null || report.SelectedItem.Text ==
"Report Type 1") {
string sql_query = "SQL QUERY";
}
Because you define sql_query within the if conditional, sql_query is
destroyed at the end of the block (the closing brace of the if statement).
How frustrating! :) Could this be a problem with the scope of the sql_query
variable? Or do I have another syntax error?


Yes, you need to define the string at function scope.
Apr 17 '06 #6
Erik -

Thanks for the help. I feel like a fish out of water :) Sorry for all the
obvious questions.

I defined the string outside of the if statement thinking that would do the
trick:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
string sql_query;
if (report.SelectedItem.Text == null || report.SelectedItem.Text ==
"Report Type 1") {
sql_query = "SQL QUERY";
}

Now I am getting:
Compiler Error Message: CS0165: Use of unassigned local variable 'sql_query'

It seems instantiating the variable outside of the if statement didn't do
the trick. What am I missing?

By the way can you recommend any good books or refrence materials that would
answer these types of questions? I've been searching through google and also
reviwed several books at local book stored but it seems that many are geared
towards advanced topics or using c# outside of asp.net.

Thanks
Brad

"Erik Funkenbusch" <er**@despam-funkenbusch.com> wrote in message
news:1e***************@funkenbusch.com...
On Mon, 17 Apr 2006 13:14:07 -0400, Brad Baker wrote:
Looking at the code again, I now have:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
if (report.SelectedItem.Text == null || report.SelectedItem.Text ==
"Report Type 1") {
string sql_query = "SQL QUERY";
}


Because you define sql_query within the if conditional, sql_query is
destroyed at the end of the block (the closing brace of the if statement).
How frustrating! :) Could this be a problem with the scope of the
sql_query
variable? Or do I have another syntax error?


Yes, you need to define the string at function scope.

Apr 17 '06 #7
string sql_query = "";

20 seconds on Google with your error message.

"Brad Baker" <br**@nospam.nospam> wrote in message
news:up**************@TK2MSFTNGP04.phx.gbl...
Erik -

Thanks for the help. I feel like a fish out of water :) Sorry for all the
obvious questions.

I defined the string outside of the if statement thinking that would do
the trick:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
string sql_query;
if (report.SelectedItem.Text == null || report.SelectedItem.Text ==
"Report Type 1") {
sql_query = "SQL QUERY";
}

Now I am getting:
Compiler Error Message: CS0165: Use of unassigned local variable
'sql_query'

It seems instantiating the variable outside of the if statement didn't do
the trick. What am I missing?

By the way can you recommend any good books or refrence materials that
would answer these types of questions? I've been searching through google
and also reviwed several books at local book stored but it seems that many
are geared towards advanced topics or using c# outside of asp.net.

Thanks
Brad

"Erik Funkenbusch" <er**@despam-funkenbusch.com> wrote in message
news:1e***************@funkenbusch.com...
On Mon, 17 Apr 2006 13:14:07 -0400, Brad Baker wrote:
Looking at the code again, I now have:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
if (report.SelectedItem.Text == null || report.SelectedItem.Text ==
"Report Type 1") {
string sql_query = "SQL QUERY";
}


Because you define sql_query within the if conditional, sql_query is
destroyed at the end of the block (the closing brace of the if
statement).
How frustrating! :) Could this be a problem with the scope of the
sql_query
variable? Or do I have another syntax error?


Yes, you need to define the string at function scope.


Apr 17 '06 #8
On Mon, 17 Apr 2006 14:36:50 -0400, Brad Baker wrote:
<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
string sql_query;
string sql_query = string.Empty;
if (report.SelectedItem.Text == null || report.SelectedItem.Text ==
"Report Type 1") {
sql_query = "SQL QUERY";
}

Now I am getting:
Compiler Error Message: CS0165: Use of unassigned local variable 'sql_query'
That's because you have a conditional situation. If your condition is not
met, then anywhere you use sql_query will be using an uninitialized
variable, and that could be anything.
By the way can you recommend any good books or refrence materials that would
answer these types of questions? I've been searching through google and also
reviwed several books at local book stored but it seems that many are geared
towards advanced topics or using c# outside of asp.net.


These are C# questions, not asp.net ones.
Apr 17 '06 #9
Try just making it "string sql_query = String.Empty; " and see if that
does the trick.

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
string sql_query = String.Empty;
if (report.SelectedItem.Text == null || report.SelectedItem.Text ==

"Report Type 1") {
sql_query = "SQL QUERY";
}

Apr 17 '06 #10
Jeff -

Thanks for the suggestion. That seems to have done the trick.

Best Regards,
Brad
"Jeff Dillon" <je********@hotmail.com> wrote in message
news:eU**************@TK2MSFTNGP02.phx.gbl...
string sql_query = "";

20 seconds on Google with your error message.

"Brad Baker" <br**@nospam.nospam> wrote in message
news:up**************@TK2MSFTNGP04.phx.gbl...
Erik -

Thanks for the help. I feel like a fish out of water :) Sorry for all the
obvious questions.

I defined the string outside of the if statement thinking that would do
the trick:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
string sql_query;
if (report.SelectedItem.Text == null || report.SelectedItem.Text ==
"Report Type 1") {
sql_query = "SQL QUERY";
}

Now I am getting:
Compiler Error Message: CS0165: Use of unassigned local variable
'sql_query'

It seems instantiating the variable outside of the if statement didn't do
the trick. What am I missing?

By the way can you recommend any good books or refrence materials that
would answer these types of questions? I've been searching through google
and also reviwed several books at local book stored but it seems that
many are geared towards advanced topics or using c# outside of asp.net.

Thanks
Brad

"Erik Funkenbusch" <er**@despam-funkenbusch.com> wrote in message
news:1e***************@funkenbusch.com...
On Mon, 17 Apr 2006 13:14:07 -0400, Brad Baker wrote:

Looking at the code again, I now have:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e) {
if (report.SelectedItem.Text == null || report.SelectedItem.Text ==
"Report Type 1") {
string sql_query = "SQL QUERY";
}

Because you define sql_query within the if conditional, sql_query is
destroyed at the end of the block (the closing brace of the if
statement).

How frustrating! :) Could this be a problem with the scope of the
sql_query
variable? Or do I have another syntax error?

Yes, you need to define the string at function scope.



Apr 18 '06 #11
Although the conditional should read

if (report.SelectedItem == null || report.SelectedItem.Text == "Report
Type 1")

because if no item is selected- not possible with a dropdown but could
be if you switched to a RadioSet and didn't preselect one- then if
nothing is selected SelectedItem will be null and SelectedItem.Text
will raise an exception.

Apr 18 '06 #12

"Russell" <ru*****@goisc.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Although the conditional should read

if (report.SelectedItem == null || report.SelectedItem.Text == "Report
Type 1")

because if no item is selected- not possible with a dropdown but could
be if you switched to a RadioSet and didn't preselect one- then if
nothing is selected SelectedItem will be null and SelectedItem.Text
will raise an exception.


That wouldn't be a problem...if SelectedItem is null, the condition is met
and SelectedItem.Text isn't checked (in the if statement), so no exception
will be raised.

Mythran

Apr 18 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

75
by: David MacQuigg | last post by:
Seems like we need a simple way to extend Python syntax that doesn't break existing syntax or clash with any other syntax in Python, is easy to type, easy to read, and is clearly distinct from the...
41
by: John Marshall | last post by:
How about the following, which I am almost positive has not been suggested: ----- class Klass: def __init__(self, name): self.name = name deco meth0: staticmethod def meth0(x):
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
1
by: amitbadgi | last post by:
HI i am getting the foll error while conv an asp application to asp.net Exception Details: System.Runtime.InteropServices.COMException: Syntax error in UPDATE statement. Source Error: Line...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
6
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. ===...
14
by: paresh | last post by:
Is this the valid C statement. int a,b,c; c = 5; <<< a = b = c; Can anyone throw the light on this. -Paresh
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.