REA_ANDREW
asked on
ExecuteScalar: Connection property has not been initialized.
Hi,
I have created a class, inside this class I have a function to see if a record is there or not using an execute scalar. I will show the code below, but I get the error that
ExecuteScalar: Connection property has not been initialized.
Class:
public class GalleryAdmin
{
private SqlConnection gallSqlConn = new SqlConnection();
public GalleryAdmin(SqlConnection GallSqlConn)
{
gallSqlConn = GallSqlConn;
}
public int TableCheck()
{
string tableName = "tbl_galleries";
string sql = "SELECT count(*) as TableCount FROM sysObjects WHERE xType = 'U' [Name] = " + tableName + "ORDER BY [Name]";
gallSqlConn.Open();
SqlCommand command = new SqlCommand( sql );
int Result = command.ExecuteScalar() is DBNull ? 0 : 1;
return Result;
gallSqlConn.Close();
}
}
Here is how I implement it on my page
public partial class Default2 : System.Web.UI.Page
{
SqlConnection GallConn = new SqlConnection(Configuratio nManager.C onnectionS trings["Ga lleryConn" ].Connecti onString);
protected void Page_Load(object sender, EventArgs e)
{
GalleryAdmin Admin = new GalleryAdmin(GallConn);
if (Admin.TableCheck() == 1)
{
Response.Write("It is there");
}
else
{
Response.Write("Not There");
}
}
}
Thanks in advance
Andrew
I have created a class, inside this class I have a function to see if a record is there or not using an execute scalar. I will show the code below, but I get the error that
ExecuteScalar: Connection property has not been initialized.
Class:
public class GalleryAdmin
{
private SqlConnection gallSqlConn = new SqlConnection();
public GalleryAdmin(SqlConnection
{
gallSqlConn = GallSqlConn;
}
public int TableCheck()
{
string tableName = "tbl_galleries";
string sql = "SELECT count(*) as TableCount FROM sysObjects WHERE xType = 'U' [Name] = " + tableName + "ORDER BY [Name]";
gallSqlConn.Open();
SqlCommand command = new SqlCommand( sql );
int Result = command.ExecuteScalar() is DBNull ? 0 : 1;
return Result;
gallSqlConn.Close();
}
}
Here is how I implement it on my page
public partial class Default2 : System.Web.UI.Page
{
SqlConnection GallConn = new SqlConnection(Configuratio
protected void Page_Load(object sender, EventArgs e)
{
GalleryAdmin Admin = new GalleryAdmin(GallConn);
if (Admin.TableCheck() == 1)
{
Response.Write("It is there");
}
else
{
Response.Write("Not There");
}
}
}
Thanks in advance
Andrew
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No problem, happens to us all :o)
ASKER
Apresto, remeber that sql you gavce me couple of questions ago, I am getting this now
Line 1: Incorrect syntax near 'Name'.
I have integrated it into the post above. Any ideas?
Andy
Line 1: Incorrect syntax near 'Name'.
I have integrated it into the post above. Any ideas?
Andy
ah i see, its being passed in as integer:
change it to this:
string sql = "SELECT count(*) as TableCount FROM sysObjects WHERE xType = 'U' [Name] = '" + tableName + "' ORDER BY [Name]";
and let me know if it works
change it to this:
string sql = "SELECT count(*) as TableCount FROM sysObjects WHERE xType = 'U' [Name] = '" + tableName + "' ORDER BY [Name]";
and let me know if it works
basically you just forgot the single quotes either side of the double quotes when you pass in the table name:
" + tableName + " = tablename
'" + tableName + "' = 'tablename' <!-- correct one
" + tableName + " = tablename
'" + tableName + "' = 'tablename' <!-- correct one
ASKER
Apresto, thanks. It is still giving me that error with your updated string
ASKER
is it because there should be an AND in the where clause or a comma
ah, sorry i meant to add an AND:
string sql = "SELECT count(*) as TableCount FROM sysObjects WHERE xType = 'U' AND [Name] = '" + tableName + "' ORDER BY [Name]";
before it was
WHERE xType = 'U' [Name] = ...
an AND was needed
string sql = "SELECT count(*) as TableCount FROM sysObjects WHERE xType = 'U' AND [Name] = '" + tableName + "' ORDER BY [Name]";
before it was
WHERE xType = 'U' [Name] = ...
an AND was needed
ASKER
aha, nearly there, I now get this with the inclusion of AND
Column name 'sysObjects.name' is invalid in the ORDER BY clause because it is not contained in an aggregate function and there is no GROUP BY clause.
Column name 'sysObjects.name' is invalid in the ORDER BY clause because it is not contained in an aggregate function and there is no GROUP BY clause.
ASKER
I am going to remove the order by clause as this is only returning one record, if any. Will let you know. Cheers for the continued help
ASKER
that worked. Nice one. All sorted. Cheers
ok just add one more thing to the end of the statement:
GROUP BY [Name]
so you should have:
string sql = "SELECT count(*) as TableCount FROM sysObjects WHERE xType = 'U' AND [Name] = '" + tableName + "' ORDER BY [Name] GROUP BY [Name]";
GROUP BY [Name]
so you should have:
string sql = "SELECT count(*) as TableCount FROM sysObjects WHERE xType = 'U' AND [Name] = '" + tableName + "' ORDER BY [Name] GROUP BY [Name]";
ok, no problem, your right, you dont need the order by clause, sorry didnt spot that
Glad to have helped anyway :o)
Glad to have helped anyway :o)
ASKER
yeh that worked I am using this
SqlCommand command = new SqlCommand( sql,gallSqlConn );
cheers Apresto