using OnLoad to search a database

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

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

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

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?
 
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...
 
Back
Top