?
Solved

ExecuteScalar: Connection property has not been initialized.

Posted on 2006-07-21
14
Medium Priority
?
2,031 Views
Last Modified: 2011-10-03
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(ConfigurationManager.ConnectionStrings["GalleryConn"].ConnectionString);

    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
0
Comment
Question by:REA_ANDREW
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 23

Accepted Solution

by:
apresto earned 2000 total points
ID: 17152983
Hi REA_ANDREW,

You need to set the connection property of the command objectL

gallSqlConn.Open();
SqlCommand command = new SqlCommand( sql );
command.Connection = gallSqlConn <-- Here
int Result = command.ExecuteScalar() is DBNull ? 0 : 1;

does this make a difference?

Apresto
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17152986
OH i am thinck today lol

yeh that worked I am using this

SqlCommand command = new SqlCommand( sql,gallSqlConn );

cheers Apresto
0
 
LVL 23

Expert Comment

by:apresto
ID: 17152991
No problem, happens to us all :o)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17153004
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
0
 
LVL 23

Expert Comment

by:apresto
ID: 17153023
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
0
 
LVL 23

Expert Comment

by:apresto
ID: 17153026
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
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17153029
Apresto, thanks. It is still giving me that error with your updated string
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17153035
is it because there should be an AND in the where clause or a comma
0
 
LVL 23

Expert Comment

by:apresto
ID: 17153036
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
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17153040
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.
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17153049
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
0
 
LVL 20

Author Comment

by:REA_ANDREW
ID: 17153052
that worked. Nice one. All sorted.  Cheers
0
 
LVL 23

Expert Comment

by:apresto
ID: 17153053
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]";
0
 
LVL 23

Expert Comment

by:apresto
ID: 17153060
ok, no problem, your right, you dont need the order by clause, sorry didnt spot that

Glad to have helped anyway :o)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question