Solved

ExecuteScalar: Connection property has not been initialized.

Posted on 2006-07-21
14
2,030 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 500 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

630 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