Syntax Problem with If statement

B

Brad Baker

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
 
M

Mythran

Brad Baker said:
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
 
G

Guest

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
 
B

Brad Baker

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
 
E

Erik Funkenbusch

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.
 
B

Brad Baker

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
 
E

Erik Funkenbusch

<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.
 
S

silverforge

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";
}
 
B

Brad Baker

Jeff -

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

Best Regards,
Brad
 
R

Russell

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.
 
M

Mythran

Russell said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top