amillyard
asked on
ExecuteScalar requires an open and available Connection. The connection's current state is closed.
Development Platform: c#, asp.net 2.x, Visual Studio Pro utilising Web Developer, IIS 6, SQL 2005 server.
The following scripting is producing the following error -- any thoughts as to why?
ExecuteScalar requires an open and available Connection. The connection's current state is closed.
__________________________ __________ __________ __________ __________ __________ ____
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
int CompanyAgent_ID;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa taSource.C onnectionS tring))
{
SqlCommand scCommand = new SqlCommand(String.Format(" SELECT [CompanyAgent_ID] FROM [StaffMember] WHERE ([ASPnetUserName] = '{0}'", Login_FORTUNE.UserName), FORTUNEConnection1);
CompanyAgent_ID = (int)scCommand.ExecuteScal ar();
FORTUNEConnection1.Close() ;
}
your time and effort with this enquiry is much apprieated.
many thanks,
The following scripting is producing the following error -- any thoughts as to why?
ExecuteScalar requires an open and available Connection. The connection's current state is closed.
__________________________
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
int CompanyAgent_ID;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa
{
SqlCommand scCommand = new SqlCommand(String.Format("
CompanyAgent_ID = (int)scCommand.ExecuteScal
FORTUNEConnection1.Close()
}
your time and effort with this enquiry is much apprieated.
many thanks,
ASKER
Thank you for your response.
Made the adjustments as suggested -- I am now getting the following error message when compiling as follows:
'System.Data.SqlClient.Sql Connection ' does not contain a definition for 'open'
Made the adjustments as suggested -- I am now getting the following error message when compiling as follows:
'System.Data.SqlClient.Sql
thts a typo
use capital "O"
.Open()
use capital "O"
.Open()
FORTUNEConnection1.Open();
ASKER
Yes, it was a typo -- thanks for confirming.
Have updated, and the script compiles now ok.
There is an error as follows:
The ConnectionString property has not been initialized.
Have updated, and the script compiles now ok.
There is an error as follows:
The ConnectionString property has not been initialized.
check whether this has the proper connection string
FORTUNESQLDataSource.Conne ctionStrin g !!!!
FORTUNESQLDataSource.Conne
ASKER
ok -- I must have deleted accidentally when cutting and pasting section of code.
thank you for keeping me on track with this issue.
when running the program -- I am getting the following syntax error as follows (i.e. this line is highlighted by debugger):
CompanyAgent_ID = (int)scCommand.ExecuteScal ar();
thank you for keeping me on track with this issue.
when running the program -- I am getting the following syntax error as follows (i.e. this line is highlighted by debugger):
CompanyAgent_ID = (int)scCommand.ExecuteScal
ASKER
SQL Syntax error
try running the query in SQL and see if u are getting the desired Output..
your query is wrong ...use this line instead...
SqlCommand scCommand = new SqlCommand(String.Format(" SELECT [CompanyAgent_ID] FROM [StaffMember] WHERE ([ASPnetUserName] = '{0}')", Login_FORTUNE.UserName), FORTUNEConnection1);
SqlCommand scCommand = new SqlCommand(String.Format("
there was a bracket missing...you started one after the WHERE clause and you did not end it...
ASKER
getting error (running sql script direct on sql server) as follows:
Incorrect syntax near '{0}'.
Incorrect syntax near '{0}'.
can u post the scrip u ran on the server
use the statement i gave earlier...there is a bracket missing in your query...
this is the proper statement(which was also give few replies before)
SqlCommand scCommand = new SqlCommand(String.Format(" SELECT [CompanyAgent_ID] FROM [StaffMember] WHERE ([ASPnetUserName] = '{0}')", Login_FORTUNE.UserName), FORTUNEConnection1);
SqlCommand scCommand = new SqlCommand(String.Format("
@gauthampj - it is clear from the query that a bracket is missing so i think that is the problem...he is not able to execute the query is SQL window because he is executing it with {0} and not by passing the value...
@amillyard, did you try this ?
SqlCommand scCommand = new SqlCommand(String.Format(" SELECT [CompanyAgent_ID] FROM [StaffMember] WHERE ([ASPnetUserName] = '{0}')", Login_FORTUNE.UserName), FORTUNEConnection1);
@amillyard, did you try this ?
SqlCommand scCommand = new SqlCommand(String.Format("
ASKER
Yes, that seems to have worked ok.
I seem to have an type issue on this line (near the very end) --
CompanyAgentName_ID = (string)scCommand.ExecuteS calar();
the first database connection retrieves the CompanyAgent the logged-in user is assigned too...i.e. an integer number.
the second database connection retrieves the CompanyAgentName (i.e. description - name) -- by searching the next table in database ... looking for the number retrieved from the 1st database check and search that against the 2nd search and then retrieving the name (description).
this description field is then written to a session variable.
as mentioned above -- the 2nd database search code is not quite there... but am I doing database lookup correctly (efficiently as well) -- by opening and closing 2 databases sessions and searching for each parameter seperately?
__________________________ __________ __________ _________
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
FORTUNESQLDataSource.Conne ctionStrin g = ConfigurationManager.Conne ctionStrin gs["FORTUN EConnectio nString"]. ToString() ;
int CompanyAgent_ID;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa taSource.C onnectionS tring))
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand(String.Format(" SELECT [CompanyAgent_ID] FROM [StaffMember] WHERE ([ASPnetUserName] = '{0}')", Login_FORTUNE.UserName), FORTUNEConnection1);
CompanyAgent_ID = (int)scCommand.ExecuteScal ar();
FORTUNEConnection1.Close() ;
}
string CompanyAgentName_ID;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa taSource.C onnectionS tring))
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand(String.Format(" SELECT [CompanyAgent_ID], [CompanyAgentName] FROM [CompanyAgents] WHERE ([CompanyAgent_ID] = '{0}')", CompanyAgent_ID), FORTUNEConnection1);
CompanyAgentName_ID = (string)scCommand.ExecuteS calar();
FORTUNEConnection1.Close() ;
}
Session["fortuneCompanyAge nt"] = CompanyAgentName_ID;
I seem to have an type issue on this line (near the very end) --
CompanyAgentName_ID = (string)scCommand.ExecuteS
the first database connection retrieves the CompanyAgent the logged-in user is assigned too...i.e. an integer number.
the second database connection retrieves the CompanyAgentName (i.e. description - name) -- by searching the next table in database ... looking for the number retrieved from the 1st database check and search that against the 2nd search and then retrieving the name (description).
this description field is then written to a session variable.
as mentioned above -- the 2nd database search code is not quite there... but am I doing database lookup correctly (efficiently as well) -- by opening and closing 2 databases sessions and searching for each parameter seperately?
__________________________
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
FORTUNESQLDataSource.Conne
int CompanyAgent_ID;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand(String.Format("
CompanyAgent_ID = (int)scCommand.ExecuteScal
FORTUNEConnection1.Close()
}
string CompanyAgentName_ID;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand(String.Format("
CompanyAgentName_ID = (string)scCommand.ExecuteS
FORTUNEConnection1.Close()
}
Session["fortuneCompanyAge
this is a wrong method that you are following use a join instead lijke
remove your last (FULL) code that you posted and use this alone instead...
string CompanyAgentName_ID;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa taSource.C onnectionS tring))
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand((String.Format( "SELECT b.[CompanyAgentName] FROM [StaffMember] a,[CompanyAgents] b WHERE a.[CompanyAgent_ID] = b.[CompanyAgent_ID] and a.[ASPnetUserName] = '{0}'", Login_FORTUNE.UserName), FORTUNEConnection1);
CompanyAgentName_ID = (string)scCommand.ExecuteS calar();
FORTUNEConnection1.Close() ;
}
Session["fortuneCompanyAge nt"] = CompanyAgentName_ID;
remove your last (FULL) code that you posted and use this alone instead...
string CompanyAgentName_ID;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand((String.Format(
CompanyAgentName_ID = (string)scCommand.ExecuteS
FORTUNEConnection1.Close()
}
Session["fortuneCompanyAge
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
what a pleasure, jaiganeshsrinivasan -- you really have hit the nail -- just checked and worked first time (after adding an extra bracket)
apprieate your generous time and efforts with this enquiry,
-many thanks :-)
updated script (with extra bracket)
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
FORTUNESQLDataSource.Conne ctionStrin g = ConfigurationManager.Conne ctionStrin gs["FORTUN EConnectio nString"]. ToString() ;
int CompanyAgent_ID;
string CompanyAgentName_ID;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa taSource.C onnectionS tring))
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand((String.Format( "SELECT b.[CompanyAgentName] FROM [StaffMember] a,[CompanyAgents] b WHERE a.[CompanyAgent_ID] = b.[CompanyAgent_ID] and a.[ASPnetUserName] = '{0}'", Login_FORTUNE.UserName)), FORTUNEConnection1);
CompanyAgentName_ID = (string)scCommand.ExecuteS calar();
FORTUNEConnection1.Close() ;
}
Session["fortuneCompanyAge nt"] = CompanyAgentName_ID;
apprieate your generous time and efforts with this enquiry,
-many thanks :-)
updated script (with extra bracket)
SqlDataSource FORTUNESQLDataSource = new SqlDataSource();
FORTUNESQLDataSource.Conne
int CompanyAgent_ID;
string CompanyAgentName_ID;
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa
{
FORTUNEConnection1.Open();
SqlCommand scCommand = new SqlCommand((String.Format(
CompanyAgentName_ID = (string)scCommand.ExecuteS
FORTUNEConnection1.Close()
}
Session["fortuneCompanyAge
using (SqlConnection FORTUNEConnection1 = new SqlConnection(FORTUNESQLDa
{
FORTUNEConnection1.open();
SqlCommand scCommand = new SqlCommand(String.Format("
CompanyAgent_ID = (int)scCommand.ExecuteScal
FORTUNEConnection1.Close()
}