Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using OnLoad to search a database

0 views
Skip to first unread message

Bill

unread,
Oct 8, 2007, 2:34:02 PM10/8/07
to
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

unread,
Oct 11, 2007, 9:40:04 AM10/11/07
to

anyone?

Mark Rae [MVP]

unread,
Oct 11, 2007, 10:23:55 AM10/11/07
to
"Bill" <antii...@gmail.com> wrote in message
news:1191868442....@g4g2000hsf.googlegroups.com...

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


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

Bill

unread,
Oct 11, 2007, 11:42:31 AM10/11/07
to
On Oct 11, 9:23 am, "Mark Rae [MVP]" <m...@markNOSPAMrae.net> wrote:
> "Bill" <antiiner...@gmail.com> wrote in message

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]

unread,
Oct 11, 2007, 2:26:11 PM10/11/07
to
"Bill" <antii...@gmail.com> wrote in message
news:1192117351.8...@y42g2000hsy.googlegroups.com...

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

0 new messages