Solved

ExecuteScalar: Connection property has not been initialized.

Posted on 2006-07-21
14
2,021 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
  • 7
  • 7
14 Comments
 
LVL 23

Accepted Solution

by:
apresto earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
No problem, happens to us all :o)
0
 
LVL 20

Author Comment

by:REA_ANDREW
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Apresto, thanks. It is still giving me that error with your updated string
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 20

Author Comment

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

Expert Comment

by:apresto
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
that worked. Nice one. All sorted.  Cheers
0
 
LVL 23

Expert Comment

by:apresto
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now