By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

Syntax Problem with If statement

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.