Can i pass a SqlConnection object with opened connection to a method/function as a parameter?

  • Thread starter Thread starter Parco
  • Start date Start date
P

Parco

Can i pass a SqlConnection object with opened connection to a
method/function as a parameter?

For example, i need a method to process something from ASP.NET by a .NET
assembly, but i don't want to connect a new connection anymore and just use
the existed opened sqlconnection object to do it and pass the object to the
method.

that is a static method.
but iis told me an exception error that is System.InvalidCastException


why?
and how to solved?
or how to do it?
 
This should be fine, can you show us the code of how you pass the connection and what you do with it when you get it

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

Can i pass a SqlConnection object with opened connection to a
method/function as a parameter?

For example, i need a method to process something from ASP.NET by a .NET
assembly, but i don't want to connect a new connection anymore and just use
the existed opened sqlconnection object to do it and pass the object to the
method.

that is a static method.
but iis told me an exception error that is System.InvalidCastException


why?
and how to solved?
or how to do it?



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
This is my assembly object's method:


public static bool CheckPermission(SqlConnection objconn, string
permissionitem, string itemname, string loginuser)
{
SqlCommand objcmd = new SqlCommand("EXEC [checkpermission]
@permissionitem = '" + DataTools.EscapeMask(permissionitem) + "', @itemname
= '" + DataTools.EscapeMask(itemname) + "', @loginacc = '" +
DataTools.EscapeMask(GetLoginUser(loginuser, LoginAccount)) + "', @logincus
= '" + DataTools.EscapeMask(GetLoginUser(loginuser, LoginCustomer)) + "'",
(SqlConnection)objconn);
short intresult = (short)objcmd.ExecuteScalar();
bool checkresult;

if(intresult > 0)
{
checkresult = true;
}
else
{
checkresult = false;
}

objcmd.Dispose();

return checkresult;
}


This is my code of ASP.NET page which using the method:


SqlConnection objconn = new
SqlConnection(ConfigurationSettings.AppSettings["connstr_webhosting"]);
objconn.Open();

bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo",
"yes", User.Identity.Name);


How to solve it?


Richard Blewett said:
This should be fine, can you show us the code of how you pass the
connection and what you do with it when you get it
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
Can i pass a SqlConnection object with opened connection to a
method/function as a parameter?

For example, i need a method to process something from ASP.NET by a .NET
assembly, but i don't want to connect a new connection anymore and just use
the existed opened sqlconnection object to do it and pass the object to the
method.

that is a static method.
but iis told me an exception error that is System.InvalidCastException


why?
and how to solved?
or how to do it?



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
Ummm OK, and which line throws the invalid cast exception?

Why do you thing this is to do with the SqlConnection? It could be in GetLoginUser, DataTools.EscapeMask or that the stored procedure doesn't return a short as you have coded. What type doe the stored procedure return?

I have to warn you that this isn't a great way to do data access. For a start the database is actually going to execute

sp_executeSql(EXEC checkpermission ...)

So you should do
SqlCommand objcmd = new SqlCommand("[checkpermission]");
objcmd.CommandType = CommandType.StoredProcedure;

The add the parameters via the parameters collection as you also have the possibility of a SQL injection attack by using string concatenation (although luckily your strings don't appear based on, at least directly, user input).

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<#[email protected]>

This is my assembly object's method:


public static bool CheckPermission(SqlConnection objconn, string
permissionitem, string itemname, string loginuser)
{
SqlCommand objcmd = new SqlCommand("EXEC [checkpermission]
@permissionitem = '" + DataTools.EscapeMask(permissionitem) + "', @itemname
= '" + DataTools.EscapeMask(itemname) + "', @loginacc = '" +
DataTools.EscapeMask(GetLoginUser(loginuser, LoginAccount)) + "', @logincus
= '" + DataTools.EscapeMask(GetLoginUser(loginuser, LoginCustomer)) + "'",
(SqlConnection)objconn);
short intresult = (short)objcmd.ExecuteScalar();
bool checkresult;

if(intresult > 0)
{
checkresult = true;
}
else
{
checkresult = false;
}

objcmd.Dispose();

return checkresult;
}


This is my code of ASP.NET page which using the method:


SqlConnection objconn = new
SqlConnection(ConfigurationSettings.AppSettings["connstr_webhosting"]);
objconn.Open();

bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo",
"yes", User.Identity.Name);


How to solve it?


Richard Blewett said:
This should be fine, can you show us the code of how you pass the
connection and what you do with it when you get it
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
Can i pass a SqlConnection object with opened connection to a
method/function as a parameter?

For example, i need a method to process something from ASP.NET by a .NET
assembly, but i don't want to connect a new connection anymore and just use
the existed opened sqlconnection object to do it and pass the object to the
method.

that is a static method.
but iis told me an exception error that is System.InvalidCastException


why?
and how to solved?
or how to do it?



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
those static methods like GetLoginUser, DataTools.EscapeMask are just for
string editing, and do not relate to the sql objects

and i have tried your methods, but still get the same error, why? how to
solve?





Richard Blewett said:
Ummm OK, and which line throws the invalid cast exception?

Why do you thing this is to do with the SqlConnection? It could be in
GetLoginUser, DataTools.EscapeMask or that the stored procedure doesn't
return a short as you have coded. What type doe the stored procedure return?
I have to warn you that this isn't a great way to do data access. For a
start the database is actually going to execute
sp_executeSql(EXEC checkpermission ...)

So you should do
SqlCommand objcmd = new SqlCommand("[checkpermission]");
objcmd.CommandType = CommandType.StoredProcedure;

The add the parameters via the parameters collection as you also have the
possibility of a SQL injection attack by using string concatenation
(although luckily your strings don't appear based on, at least directly,
user input).
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
This is my assembly object's method:


public static bool CheckPermission(SqlConnection objconn, string
permissionitem, string itemname, string loginuser)
{
SqlCommand objcmd = new SqlCommand("EXEC [checkpermission]
@permissionitem = '" + DataTools.EscapeMask(permissionitem) + "', @itemname
= '" + DataTools.EscapeMask(itemname) + "', @loginacc = '" +
DataTools.EscapeMask(GetLoginUser(loginuser, LoginAccount)) + "', @logincus
= '" + DataTools.EscapeMask(GetLoginUser(loginuser, LoginCustomer)) + "'",
(SqlConnection)objconn);
short intresult = (short)objcmd.ExecuteScalar();
bool checkresult;

if(intresult > 0)
{
checkresult = true;
}
else
{
checkresult = false;
}

objcmd.Dispose();

return checkresult;
}


This is my code of ASP.NET page which using the method:


SqlConnection objconn = new
SqlConnection(ConfigurationSettings.AppSettings["connstr_webhosting"]);
objconn.Open();

bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo",
"yes", User.Identity.Name);


How to solve it?


Richard Blewett said:
This should be fine, can you show us the code of how you pass the
connection and what you do with it when you get it
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
Can i pass a SqlConnection object with opened connection to a
method/function as a parameter?

For example, i need a method to process something from ASP.NET by a ..NET
assembly, but i don't want to connect a new connection anymore and just use
the existed opened sqlconnection object to do it and pass the object to the
method.

that is a static method.
but iis told me an exception error that is System.InvalidCastException


why?
and how to solved?
or how to do it?



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
What line of code generates the exception? I'm guessing its the cast to short from the ExecuteScalar

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

those static methods like GetLoginUser, DataTools.EscapeMask are just for
string editing, and do not relate to the sql objects

and i have tried your methods, but still get the same error, why? how to
solve?





Richard Blewett said:
Ummm OK, and which line throws the invalid cast exception?

Why do you thing this is to do with the SqlConnection? It could be in
GetLoginUser, DataTools.EscapeMask or that the stored procedure doesn't
return a short as you have coded. What type doe the stored procedure return?
I have to warn you that this isn't a great way to do data access. For a
start the database is actually going to execute
sp_executeSql(EXEC checkpermission ...)

So you should do
SqlCommand objcmd = new SqlCommand("[checkpermission]");
objcmd.CommandType = CommandType.StoredProcedure;

The add the parameters via the parameters collection as you also have the
possibility of a SQL injection attack by using string concatenation
(although luckily your strings don't appear based on, at least directly,
user input).
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
This is my assembly object's method:


public static bool CheckPermission(SqlConnection objconn, string
permissionitem, string itemname, string loginuser)
{
SqlCommand objcmd = new SqlCommand("EXEC [checkpermission]
@permissionitem = '" + DataTools.EscapeMask(permissionitem) + "', @itemname
= '" + DataTools.EscapeMask(itemname) + "', @loginacc = '" +
DataTools.EscapeMask(GetLoginUser(loginuser, LoginAccount)) + "', @logincus
= '" + DataTools.EscapeMask(GetLoginUser(loginuser, LoginCustomer)) + "'",
(SqlConnection)objconn);
short intresult = (short)objcmd.ExecuteScalar();
bool checkresult;

if(intresult > 0)
{
checkresult = true;
}
else
{
checkresult = false;
}

objcmd.Dispose();

return checkresult;
}


This is my code of ASP.NET page which using the method:


SqlConnection objconn = new
SqlConnection(ConfigurationSettings.AppSettings["connstr_webhosting"]);
objconn.Open();

bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo",
"yes", User.Identity.Name);


How to solve it?


Richard Blewett said:
This should be fine, can you show us the code of how you pass the
connection and what you do with it when you get it
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
Can i pass a SqlConnection object with opened connection to a
method/function as a parameter?

For example, i need a method to process something from ASP.NET by a .NET
assembly, but i don't want to connect a new connection anymore and just use
the existed opened sqlconnection object to do it and pass the object to the
method.

that is a static method.
but iis told me an exception error that is System.InvalidCastException


why?
and how to solved?
or how to do it?



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
This line of code generates the exception:

bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo", "yes",
User.Identity.Name);




Richard Blewett said:
What line of code generates the exception? I'm guessing its the cast to short from the ExecuteScalar

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
those static methods like GetLoginUser, DataTools.EscapeMask are just for
string editing, and do not relate to the sql objects

and i have tried your methods, but still get the same error, why? how to
solve?





Richard Blewett said:
Ummm OK, and which line throws the invalid cast exception?

Why do you thing this is to do with the SqlConnection? It could be in
GetLoginUser, DataTools.EscapeMask or that the stored procedure doesn't
return a short as you have coded. What type doe the stored procedure return?
I have to warn you that this isn't a great way to do data access. For a
start the database is actually going to execute
sp_executeSql(EXEC checkpermission ...)

So you should do
SqlCommand objcmd = new SqlCommand("[checkpermission]");
objcmd.CommandType = CommandType.StoredProcedure;

The add the parameters via the parameters collection as you also have
the
possibility of a SQL injection attack by using string concatenation
(although luckily your strings don't appear based on, at least directly,
user input).
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
This is my assembly object's method:


public static bool CheckPermission(SqlConnection objconn, string
permissionitem, string itemname, string loginuser)
{
SqlCommand objcmd = new SqlCommand("EXEC [checkpermission]
@permissionitem = '" + DataTools.EscapeMask(permissionitem) + "', @itemname
= '" + DataTools.EscapeMask(itemname) + "', @loginacc = '" +
DataTools.EscapeMask(GetLoginUser(loginuser, LoginAccount)) + "', @logincus
= '" + DataTools.EscapeMask(GetLoginUser(loginuser, LoginCustomer)) + "'",
(SqlConnection)objconn);
short intresult = (short)objcmd.ExecuteScalar();
bool checkresult;

if(intresult > 0)
{
checkresult = true;
}
else
{
checkresult = false;
}

objcmd.Dispose();

return checkresult;
}


This is my code of ASP.NET page which using the method:


SqlConnection objconn = new
SqlConnection(ConfigurationSettings.AppSettings["connstr_webhosting"]);
objconn.Open();

bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo",
"yes", User.Identity.Name);


How to solve it?


Richard Blewett said:
This should be fine, can you show us the code of how you pass the
connection and what you do with it when you get it
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
Can i pass a SqlConnection object with opened connection to a
method/function as a parameter?

For example, i need a method to process something from ASP.NET by a .NET
assembly, but i don't want to connect a new connection anymore and
just
use
the existed opened sqlconnection object to do it and pass the object
to
the
method.

that is a static method.
but iis told me an exception error that is System.InvalidCastException


why?
and how to solved?
or how to do it?



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
Parco said:
This line of code generates the exception:

bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo", "yes",
User.Identity.Name);

That would suggest that CPanel.CheckPermission isn't returning a bool.
I suggest you find out what it is returning:

object foo = CPanel.CheckPermission(objconn, "systeminfo", "yes",
User.Identity.Name);

// Or whatever diagnostic you want to use
Console.WriteLine (foo.GetType());
 
The only thing in *that* line of code that would cause thatexception is the cast to bool - the CheckPermission method you posted before returned a bool - so why are you casting?

Would it be possible to strip down the code into a simple console app that demonstrates the problem that maybe I could run and debug.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

This line of code generates the exception:

bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo", "yes",
User.Identity.Name);




Richard Blewett said:
What line of code generates the exception? I'm guessing its the cast to short from the ExecuteScalar

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
those static methods like GetLoginUser, DataTools.EscapeMask are just for
string editing, and do not relate to the sql objects

and i have tried your methods, but still get the same error, why? how to
solve?





Richard Blewett said:
Ummm OK, and which line throws the invalid cast exception?

Why do you thing this is to do with the SqlConnection? It could be in
GetLoginUser, DataTools.EscapeMask or that the stored procedure doesn't
return a short as you have coded. What type doe the stored procedure return?
I have to warn you that this isn't a great way to do data access. For a
start the database is actually going to execute
sp_executeSql(EXEC checkpermission ...)

So you should do
SqlCommand objcmd = new SqlCommand("[checkpermission]");
objcmd.CommandType = CommandType.StoredProcedure;

The add the parameters via the parameters collection as you also have
the
possibility of a SQL injection attack by using string concatenation
(although luckily your strings don't appear based on, at least directly,
user input).
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
This is my assembly object's method:


public static bool CheckPermission(SqlConnection objconn, string
permissionitem, string itemname, string loginuser)
{
SqlCommand objcmd = new SqlCommand("EXEC [checkpermission]
@permissionitem = '" + DataTools.EscapeMask(permissionitem) + "', @itemname
= '" + DataTools.EscapeMask(itemname) + "', @loginacc = '" +
DataTools.EscapeMask(GetLoginUser(loginuser, LoginAccount)) + "', @logincus
= '" + DataTools.EscapeMask(GetLoginUser(loginuser, LoginCustomer)) + "'",
(SqlConnection)objconn);
short intresult = (short)objcmd.ExecuteScalar();
bool checkresult;

if(intresult > 0)
{
checkresult = true;
}
else
{
checkresult = false;
}

objcmd.Dispose();

return checkresult;
}


This is my code of ASP.NET page which using the method:


SqlConnection objconn = new
SqlConnection(ConfigurationSettings.AppSettings["connstr_webhosting"]);
objconn.Open();

bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo",
"yes", User.Identity.Name);


How to solve it?


Richard Blewett said:
This should be fine, can you show us the code of how you pass the
connection and what you do with it when you get it
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
Can i pass a SqlConnection object with opened connection to a
method/function as a parameter?

For example, i need a method to process something from ASP.NET by a .NET
assembly, but i don't want to connect a new connection anymore and
just
use
the existed opened sqlconnection object to do it and pass the object
to
the
method.

that is a static method.
but iis told me an exception error that is System.InvalidCastException


why?
and how to solved?
or how to do it?



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
No, it must be returned a bool. you may refer the codes those i have given
you before and you will know/

that it a independent bool variable and use "if" statement to give a real
true/false value to the variable directly.
And the variable is returned directly.
 
What you have posted and the error you get don't correlate if what you have posted is the exact situation. As Jon and me have both pointed out, if *that* line occurs with *that* exception then it must be the cast to bool - but you say its not that. So can you please cut down your code into a bare skeleton that shows the problem (one without your EscapeMask, GetLoginUser, etc) so we can take the example offline and work out what it going wrong. At the moment we have no way to diagnose the problem

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

No, it must be returned a bool. you may refer the codes those i have given
you before and you will know/

that it a independent bool variable and use "if" statement to give a real
true/false value to the variable directly.
And the variable is returned directly.






Jon Skeet said:
That would suggest that CPanel.CheckPermission isn't returning a bool.
I suggest you find out what it is returning:

object foo = CPanel.CheckPermission(objconn, "systeminfo", "yes",
User.Identity.Name);

// Or whatever diagnostic you want to use
Console.WriteLine (foo.GetType());



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
Looks functional.
Byzantine.
But functional.

I agree with Richard... set up your command object to execute a stored
procedure and create the parameters individually.

Then, at least, if you are getting a casting error from one of the other
methods in your long concatenated (rather tortured) string, you will be able
to tell which one! I also agree that the problem is probably either in the
DataTools.EscapeMask method (a static method, apparently), or the
GetLoginUser method. It probably has nothing to do with the SqlConnection
object.

--- Nick

Parco said:
This is my assembly object's method:


public static bool CheckPermission(SqlConnection objconn, string
permissionitem, string itemname, string loginuser)
{
SqlCommand objcmd = new SqlCommand("EXEC [checkpermission]
@permissionitem = '" + DataTools.EscapeMask(permissionitem) + "', @itemname
= '" + DataTools.EscapeMask(itemname) + "', @loginacc = '" +
DataTools.EscapeMask(GetLoginUser(loginuser, LoginAccount)) + "', @logincus
= '" + DataTools.EscapeMask(GetLoginUser(loginuser, LoginCustomer)) + "'",
(SqlConnection)objconn);
short intresult = (short)objcmd.ExecuteScalar();
bool checkresult;

if(intresult > 0)
{
checkresult = true;
}
else
{
checkresult = false;
}

objcmd.Dispose();

return checkresult;
}


This is my code of ASP.NET page which using the method:


SqlConnection objconn = new
SqlConnection(ConfigurationSettings.AppSettings["connstr_webhosting"]);
objconn.Open();

bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo",
"yes", User.Identity.Name);


How to solve it?


Richard Blewett said:
This should be fine, can you show us the code of how you pass the
connection and what you do with it when you get it
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
Can i pass a SqlConnection object with opened connection to a
method/function as a parameter?

For example, i need a method to process something from ASP.NET by a ..NET
assembly, but i don't want to connect a new connection anymore and just use
the existed opened sqlconnection object to do it and pass the object to the
method.

that is a static method.
but iis told me an exception error that is System.InvalidCastException


why?
and how to solved?
or how to do it?



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
thank you, but now the problem just be solved by myself magically
I have tried change

short intresult = (short)objcmd.ExecuteScalar();

into

short intresult = Convert.ToInt16(objcmd.ExecuteScalar());

Then, it work now!
But anyway, really thank you all



Richard Blewett said:
What you have posted and the error you get don't correlate if what you
have posted is the exact situation. As Jon and me have both pointed out, if
*that* line occurs with *that* exception then it must be the cast to bool -
but you say its not that. So can you please cut down your code into a bare
skeleton that shows the problem (one without your EscapeMask, GetLoginUser,
etc) so we can take the example offline and work out what it going wrong. At
the moment we have no way to diagnose the problem
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
No, it must be returned a bool. you may refer the codes those i have given
you before and you will know/

that it a independent bool variable and use "if" statement to give a real
true/false value to the variable directly.
And the variable is returned directly.






Jon Skeet said:
That would suggest that CPanel.CheckPermission isn't returning a bool.
I suggest you find out what it is returning:

object foo = CPanel.CheckPermission(objconn, "systeminfo", "yes",
User.Identity.Name);

// Or whatever diagnostic you want to use
Console.WriteLine (foo.GetType());



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
This means that the thing the stored procedure returned was not a short, but had a valid way to be converted to a short

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.framework/<[email protected]>

thank you, but now the problem just be solved by myself magically
I have tried change

short intresult = (short)objcmd.ExecuteScalar();

into

short intresult = Convert.ToInt16(objcmd.ExecuteScalar());

Then, it work now!
But anyway, really thank you all



Richard Blewett said:
What you have posted and the error you get don't correlate if what you
have posted is the exact situation. As Jon and me have both pointed out, if
*that* line occurs with *that* exception then it must be the cast to bool -
but you say its not that. So can you please cut down your code into a bare
skeleton that shows the problem (one without your EscapeMask, GetLoginUser,
etc) so we can take the example offline and work out what it going wrong. At
the moment we have no way to diagnose the problem
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework/ said:
No, it must be returned a bool. you may refer the codes those i have given
you before and you will know/

that it a independent bool variable and use "if" statement to give a real
true/false value to the variable directly.
And the variable is returned directly.






Jon Skeet said:
That would suggest that CPanel.CheckPermission isn't returning a bool.
I suggest you find out what it is returning:

object foo = CPanel.CheckPermission(objconn, "systeminfo", "yes",
User.Identity.Name);

// Or whatever diagnostic you want to use
Console.WriteLine (foo.GetType());



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004



[microsoft.public.dotnet.framework]
 
Back
Top