Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

using OnLoad to search a database

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill - 08 Oct 2007 19:34 GMT
Hi all

I want to use an OnLoad command to trigger a search of a database to
see if the user's login is there. I then want to set a variable which
indicates if the user was found.

Do I use a SelectCommand in the SqlDataSource and reference
User.Identity.Name? If so, what is the proper syntax for that?

Then how do I use the OnLoad command to trigger the search?

Any help appreciated!

Thanks,
Bill
Bill - 11 Oct 2007 14:40 GMT
> Hi all
>
[quoted text clipped - 11 lines]
> Thanks,
> Bill

anyone?
Mark Rae [MVP] - 11 Oct 2007 15:23 GMT
> I want to use an OnLoad command to trigger a search of a database to
> see if the user's login is there.

Presumably, you mean the Page_Load method...?

> I then want to set a variable which indicates if the user was found.

OK.

> Do I use a SelectCommand in the SqlDataSource and reference
> User.Identity.Name?

If that is how your users are referenced in your database... You could also
use a stored procedure...

> Then how do I use the OnLoad command to trigger the search?

protected void Page_Load(object sender, EventArgs e)
{
   strConnectionString = "<connection string>";
   strSQL = "SELECT * FROM MyUsers WHERE UserID = '" + User.Identity.Name +
"'";

   using (SqlConnection objSqlConnection = new
SqlConnection(mstrConnectionString))
   {
       objSqlConnection.Open();
       using (SqlCommand objSqlCommand = new SqlCommand(pstrSQL,
objSqlConnection))
       {
           using (SqlDataAdapter objDA = new SqlDataAdapter(objSqlCommand))
           {
               using (DataSet objDataSet = new DataSet())
               {
                   objDA.Fill(objDataSet);
                   if (objDataSet.Tables[0].Rows.Count > 0)
                   {
                       // get any additional user details...
                       Session["UserExists"] = true;
                   }
               }
           }
       }
   }
}

The above is merely one of several methods you could use, and is not
particularly robust, but it should get you started...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Bill - 11 Oct 2007 16:42 GMT
> > I want to use an OnLoad command to trigger a search of a database to
> > see if the user's login is there.
[quoted text clipped - 49 lines]
> Mark Rae
> ASP.NET MVPhttp://www.markrae.net

Thanks, Mark. This should get me started.

Does the DataSet and DataAdapter code create the DataSet and
DataAdapters on their own? Or do I need to drag those elements in from
the toolbox and link them?
Mark Rae [MVP] - 11 Oct 2007 19:26 GMT
> Does the DataSet and DataAdapter code create the DataSet and
> DataAdapters on their own?

Yes.

> Or do I need to drag those elements in from the toolbox and link them?

No. FWIW, I never use the ToolBox for anything at all...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.