Solved

Simple IF Statement C#.. NEED HELP!

Posted on 2009-07-03
25
228 Views
Last Modified: 2012-05-07
Hello,

I'm trying to do an IF Statement in C# (.net). I want to call on the IF Statement, if SectorID is not 1 then response.redirect...

SectorID is in the database.. how do I do this?
DataModule dm = new DataModule();
 
                DataTable dt2 = dm.getData(String.Format("select ID, SectorID from tbl_UploadStories where ID={0} AND SectorID = 1", ar.SectionID.ToString()));
 
                if (dt2.DataSet != WHAT TO I PUT HERE??? )
                {
                    Response.Redirect("/");
                }

Open in new window

0
Comment
Question by:Fezi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 5
  • +1
25 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 24771430
if (dt2.Rows[0]["ID"] != 1 )
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 450 total points
ID: 24771439
I presume:
if (dt2.Rows.Count == 0 )
                {
                    Response.Redirect("/");
                }

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24771468
Like angelll presumed: does your query always return 1 row ?
If so, you can use the check of angelll.
         if (dt2.Rows.Count == 0 )
                {
                    Response.Redirect("/");
                }

If you return more rows and you need to check the value of the "ID" of the first row, you can check it like I mentioned.
         if (dt2.Rows[0]["ID"] != "1" )
                {
                    Response.Redirect("/");
                }

Or can it return more rows and do you need to check if there is a row with the "ID" equal to 1 ?
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

Author Comment

by:Fezi
ID: 24771474
angelIII - Your code works, but not in the way I want it to work. Your code works if the ID does not exist then it will redirect.

Dhaest - your code does not seem to work, error is 'cannot be applied to operands of type 'object' and 'int''

What I want to do is, If the SectorID is not 1 then redirect


hope this helps
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 50 total points
ID: 24771479
if ((int)dt2.Rows[0]["ID"] != 1 )
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24771493
or this:
if (dt2.Rows[0]["ID"].ToString() != "1" )
                {
                    Response.Redirect("/");
                }

Open in new window

0
 

Author Comment

by:Fezi
ID: 24771521
For some reason it is only allowing me to see the first record.

Scenario:
I have a list of articles on a page, which I click on a headline it displays another page with the full article, in the database I have many different articles with different SectorID's. I only want to show articles with SectorID = 1. All other articles with SectorID 2,4 and 5 will need to redirect to the home page.

Thats what I want to do.

The code above isn't quiet there yet.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24771548
>> DataTable dt2 = dm.getData(String.Format("select ID, SectorID from tbl_UploadStories where ID={0} AND SectorID = 1", ar.SectionID.ToString()));

So you are searching in the database for all items in your database (tbl_UploadStories ) where sectorID = 1.... But why are you setting ID = ? with ar.SectionID ?
0
 

Author Comment

by:Fezi
ID: 24771558
the ID is in the querystring, if someone tries to manually enter different articles in the querystring using the ID, with SectorID = 2 in the database, then it needs to redirect to the home page
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 50 total points
ID: 24771578
So if you perform the query, you want to know if there exists a record for SectorID = 1 and ID = (article coming from querystring)
I don't see why you wouldn't use the suggestion from angellll

if dt2.Rows.Count == 0 ....
0
 

Author Comment

by:Fezi
ID: 24771643

Angel's is useful, but it's not what I want. that code only works if the record does not exist in the database.

In my database, I have Incremented ID's and SectorID  = 1, 2, 4, 5


But I only want user's to view articles with SectorID = 1.  e.g.

ID = 4
SectorID = 1

ID = 3
SectorID = 2

If the user types ID 4 in the querystring the page should display. If the user types 3 in the querystring It should REDIRECT the page to the homepage, because the SectorID = 2.

0
 
LVL 5

Expert Comment

by:mallcore
ID: 24771682
DataModule dm = new DataModule();
 
                DataTable dt2 = dm.getData(String.Format("select ID, SectorID from tbl_UploadStories where ID={0} AND SectorID = 1", ar.SectionID.ToString()));
 
                if (dt2.DataSet != 1 )
                {
                    Response.Redirect("/");
                }
0
 

Author Comment

by:Fezi
ID: 24771704
Yes, I tried this but got an error on ----        if (dt2.DataSet != 1 )

Operator '!=' cannot be applied to operands of type 'System.Data.DataSet' and 'int'
0
 
LVL 5

Expert Comment

by:mallcore
ID: 24771760
What about
if (dt2.DataSet != "1" )
0
 

Author Comment

by:Fezi
ID: 24771765
same error...
0
 

Author Comment

by:Fezi
ID: 24771792
Does anyone have a solution?
0
 
LVL 5

Expert Comment

by:mallcore
ID: 24771812
What if you take angellls suggestion and put it into a for loop?

so it would look like this

if (dt2.Rows[0]["ID"].ToString() != "1" )
                {
                    Response.Redirect("/");
                }
for(int i = 0 ; i <= dt2.Rows[i] ; i++
{
if (dt2.Rows[i]["ID"].ToString() != "1" )
                {
                    Response.Redirect("/");
                }
}

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24771936
I suggest to use the power of .net
DataView dv = dt2.DefaultView();
dv.RowFilter = "ID = 1";
if (dv.Rows.Count > 0 )
{
  Response.Redirect("/");
}

Open in new window

0
 

Author Comment

by:Fezi
ID: 24771964
hi, i get two errors with that code.

Error      101      'System.Data.DataTable.DefaultView' is a 'property' but is used like a 'method'

Error      102      'System.Data.DataView' does not contain a definition for 'Rows'

All I want to do is NOT to display SectorID = 2, 4, 5 on the page
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24772063
sorry, the first line should read:
DataView dv = dt2.DefaultView;
0
 

Author Comment

by:Fezi
ID: 24772270
hi... I still have an error with the 3rd line 'Rows'

                DataView dv = dt2.DefaultView;
                dv.RowFilter = "ID = 1";
                if (dv.Rows.Count > 0)
                {
                    Response.Redirect("/");
                }

Error      102      'System.Data.DataView' does not contain a definition for 'Rows'
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24772378
DataView dv = dt2.DefaultView;
                dv.RowFilter = "ID = 1";
                if (dv.Count > 0)
                {
                    Response.Redirect("/");
                }
0
 

Author Comment

by:Fezi
ID: 24772540
the code works, but it's not exactly what I need.

I only need want to display articles from the database that have a SectorID 1 in the database. SectorID and ID are two different fields. SectorID is like a section stored in another database. and ID is the main ID of that article...
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24772553
so, this should be it?
DataView dv = dt2.DefaultView;
                dv.RowFilter = "SectorID = 1";
                if (dv.Count == 0)
                {
                    Response.Redirect("/");
                }

Open in new window

0
 

Author Comment

by:Fezi
ID: 24772618
It still displays  the articles with different SectionID ...
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question