amillyard
asked on
c# asp.net sql read - selecting record & getting variable transferred locally
Development Platform : c#, asp.net 2.x, VIsual Studio Pro utilising Web Developer, IIS6, SQL Server 2005
I am trying to pull out a single user's (ASPnetUserName), associated CompanyAgent_ID number from the StaffMember database table as below -- then I want a local variable to equal the CompanyAgent_ID value.
The Login1.Username variable is from the login.aspx page (entry box) -- thats what I am wanting to compare with database -- ie. in selecting the correct username record.
In short, not sure if the sql statement is 100% correct and 2., how to get the CompanyAgent_ID value into a local variable.
Your time and efforts with this enquiry are much apprieated.
Thank you in advance.
SqlDataSource testSQLDataSource = new SqlDataSource();
testSQLDataSource.Connecti onString = ConfigurationManager.Conne ctionStrin gs["sqlCon nectionStr ing"].ToSt ring();
System.Data.SqlClient.SqlC ommand scCommand = new System.Data.SqlClient.SqlC ommand();
scCommand.CommandType = System.Data.CommandType.Te xt;
scCommand.CommandText = "SELECT [ASPnetUserName], [CompanyAgent_ID] FROM [StaffMember] WHERE ([ASPnetUserName] = @Login1.Username)";
scCommand.Parameters.Add(" @RETURN_VA LUE", System.Data.SqlDbType.Int) .Direction = ParameterDirection.ReturnV alue;
SqlConnection sqlConnection1 = new SqlConnection(testSQLDataS ource.Conn ectionStri ng);
try
{
sqlConnection1.Open();
scCommand.Connection = sqlConnection1;
scCommand.ExecuteNonQuery( );
}
catch (Exception ex)
{
SQL_FailureHandler(ex);
}
finally
{
sqlConnection1.Close();
Label_SubmissionStatus.Tex t = "[ Comitted to Database OK ]";
}
I am trying to pull out a single user's (ASPnetUserName), associated CompanyAgent_ID number from the StaffMember database table as below -- then I want a local variable to equal the CompanyAgent_ID value.
The Login1.Username variable is from the login.aspx page (entry box) -- thats what I am wanting to compare with database -- ie. in selecting the correct username record.
In short, not sure if the sql statement is 100% correct and 2., how to get the CompanyAgent_ID value into a local variable.
Your time and efforts with this enquiry are much apprieated.
Thank you in advance.
SqlDataSource testSQLDataSource = new SqlDataSource();
testSQLDataSource.Connecti
System.Data.SqlClient.SqlC
scCommand.CommandType = System.Data.CommandType.Te
scCommand.CommandText = "SELECT [ASPnetUserName], [CompanyAgent_ID] FROM [StaffMember] WHERE ([ASPnetUserName] = @Login1.Username)";
scCommand.Parameters.Add("
SqlConnection sqlConnection1 = new SqlConnection(testSQLDataS
try
{
sqlConnection1.Open();
scCommand.Connection = sqlConnection1;
scCommand.ExecuteNonQuery(
}
catch (Exception ex)
{
SQL_FailureHandler(ex);
}
finally
{
sqlConnection1.Close();
Label_SubmissionStatus.Tex
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry. My fault.
int CompanyAgend_ID;
using(SqlConnection sqlConnection1 = new SqlConnection(testSQLDataS ource.Conn ectionStri ng))
{
sqlConnection.Open();
SqlCommand scCommand = new SqlCommand(String.Format(" SELECT [CompanyAgent_ID] FROM [StaffMember] WHERE ([ASPnetUserName] = '{0}'", Login1.Username), sqlConnection1);
CompanyAgent_ID = (int)scCommand.ExecuteScal ar();
sqlConnection.Close();
}
int CompanyAgend_ID;
using(SqlConnection sqlConnection1 = new SqlConnection(testSQLDataS
{
sqlConnection.Open();
SqlCommand scCommand = new SqlCommand(String.Format("
CompanyAgent_ID = (int)scCommand.ExecuteScal
sqlConnection.Close();
}
Once again:
sqlConnection1.Open(); instead of sqlConnection.Open();
sqlConnection1.Open(); instead of sqlConnection.Open();
ASKER
There is an error message as follows:
ExecuteScalar requires an open and available Connection. The connection's current state is closed.
With the previously supplied code -- was I supposed to removed/replace other code. Is this supplied code all that is needed to mix and match with previous scripting etc.
cheers,