Solved

ExecuteScalar: Connection property has not been initialized.

Posted on 2006-07-21
14
2,029 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
Technology Partners: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create an MVC5, Razor Widget 2 51
asp.net web app 3 59
asp.net repeater server posting 5 39
Manage big list of parameter list 8 27
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.…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

752 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