Solved

ExecuteScalar: Connection property has not been initialized.

Posted on 2006-07-21
14
2,024 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calling stored proc in EDMX 11 24
Achieve json result 2 65
Search by two fields at the same time using c# radio button 5 39
Host asp.net pages 5 25
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…
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.…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

21 Experts available now in Live!

Get 1:1 Help Now