.NET Forum / .NET Framework / New Users / October 2004
Can i pass a SqlConnection object with opened connection to a method/function as a parameter?
|
|
Thread rating:  |
Parco - 02 Oct 2004 08:51 GMT 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?
Richard Blewett [DevelopMentor] - 02 Oct 2004 09:40 GMT 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/<ud$vTSFqEHA.2456@TK2MSFTNGP10.phx.gbl>
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?
[microsoft.public.dotnet.framework]
Parco - 02 Oct 2004 11:13 GMT 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?
> 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/<ud$vTSFqEHA.2456@TK2MSFTNGP10.phx.gbl>
> Can i pass a SqlConnection object with opened connection to a > method/function as a parameter? [quoted text clipped - 17 lines] > > [microsoft.public.dotnet.framework] Richard Blewett [DevelopMentor] - 02 Oct 2004 13:59 GMT 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/<#KKSDiGqEHA.3668@TK2MSFTNGP15.phx.gbl>
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 [DevelopMentor]" <richardb@develop.com> ?b?l?? news:uiz4%23tFqEHA.2900@TK2MSFTNGP12.phx.gbl ?????g...
> 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/<ud$vTSFqEHA.2456@TK2MSFTNGP10.phx.gbl>
> 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. > [quoted text clipped - 11 lines] > > [microsoft.public.dotnet.framework]
[microsoft.public.dotnet.framework]
Parco - 02 Oct 2004 19:25 GMT 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?
> 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 > [quoted text clipped - 5 lines] > > 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/<#KKSDiGqEHA.3668@TK2MSFTNGP15.phx.gbl>
> This is my assembly object's method: > [quoted text clipped - 46 lines] > > > > nntp://news.microsoft.com/microsoft.public.dotnet.framework/<ud$vTSFqEHA.2456@TK2MSFTNGP10.phx.gbl>
> > > > Can i pass a SqlConnection object with opened connection to a [quoted text clipped - 32 lines] > > [microsoft.public.dotnet.framework] Richard Blewett [DevelopMentor] - 02 Oct 2004 20:01 GMT 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/<O8Y7D1KqEHA.2588@TK2MSFTNGP12.phx.gbl>
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 [DevelopMentor]" <richardb@develop.com> ?b?l?? news:unHJx%23HqEHA.2484@TK2MSFTNGP09.phx.gbl ?????g...
> 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 ...) > [quoted text clipped - 3 lines] > > 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/<#KKSDiGqEHA.3668@TK2MSFTNGP15.phx.gbl>
> This is my assembly object's method: > [quoted text clipped - 3 lines] > 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(); [quoted text clipped - 34 lines] > > Richard Blewett - DevelopMentor > > http://staff.develop.com/richardb/weblog nntp://news.microsoft.com/microsoft.public.dotnet.framework/<ud$vTSFqEHA.2456@TK2MSFTNGP10.phx.gbl>
> > 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 [quoted text clipped - 22 lines] > > [microsoft.public.dotnet.framework]
[microsoft.public.dotnet.framework]
Parco - 02 Oct 2004 20:12 GMT This line of code generates the exception:
bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo", "yes", User.Identity.Name);
> 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/<O8Y7D1KqEHA.2588@TK2MSFTNGP12.phx.gbl>
> those static methods like GetLoginUser, DataTools.EscapeMask are just for > string editing, and do not relate to the sql objects [quoted text clipped - 30 lines] > > > > nntp://news.microsoft.com/microsoft.public.dotnet.framework/<#KKSDiGqEHA.3668@TK2MSFTNGP15.phx.gbl>
> > > > This is my assembly object's method: [quoted text clipped - 56 lines] > > > > > nntp://news.microsoft.com/microsoft.public.dotnet.framework/<ud$vTSFqEHA.2456@TK2MSFTNGP10.phx.gbl>
> > > > > > Can i pass a SqlConnection object with opened connection to a [quoted text clipped - 44 lines] > > [microsoft.public.dotnet.framework] Jon Skeet [C# MVP] - 02 Oct 2004 20:50 GMT > 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());
 Signature Jon Skeet - <skeet@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Parco - 02 Oct 2004 20:59 GMT 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.
> > This line of code generates the exception: > > [quoted text clipped - 9 lines] > // Or whatever diagnostic you want to use > Console.WriteLine (foo.GetType()); Richard Blewett [DevelopMentor] - 02 Oct 2004 22:35 GMT 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/<eQymhpLqEHA.868@TK2MSFTNGP10.phx.gbl>
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 [C# MVP]" <skeet@pobox.com> ??? news:MPG.1bc916e5e8586b5298b595@msnews.microsoft.com ???...
> Parco <parco@nowlover.com> wrote: > > This line of code generates the exception: > > > > bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo", "yes",
> > User.Identity.Name); > [quoted text clipped - 11 lines] > http://www.pobox.com/~skeet > If replying to the group, please do not mail me too
[microsoft.public.dotnet.framework]
Parco - 03 Oct 2004 18:30 GMT 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
> 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/<eQymhpLqEHA.868@TK2MSFTNGP10.phx.gbl>
> No, it must be returned a bool. you may refer the codes those i have given > you before and you will know/ [quoted text clipped - 31 lines] > > [microsoft.public.dotnet.framework] Richard Blewett [DevelopMentor] - 03 Oct 2004 21:14 GMT 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/<uXVRw6WqEHA.596@TK2MSFTNGP11.phx.gbl>
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 [DevelopMentor]" <richardb@develop.com> ?b?l?? news:%23M2e1eMqEHA.1164@TK2MSFTNGP10.phx.gbl ?????g...
> 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/<eQymhpLqEHA.868@TK2MSFTNGP10.phx.gbl>
> No, it must be returned a bool. you may refer the codes those i have given
> you before and you will know/ > [quoted text clipped - 30 lines] > > [microsoft.public.dotnet.framework]
[microsoft.public.dotnet.framework]
Richard Blewett [DevelopMentor] - 02 Oct 2004 20:57 GMT 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/<e3M8VPLqEHA.1308@TK2MSFTNGP14.phx.gbl>
This line of code generates the exception: bool permited = (bool)CPanel.CheckPermission(objconn, "systeminfo", "yes", User.Identity.Name); "Richard Blewett [DevelopMentor]" <richardb@develop.com> ?b?l?? news:%23cxw9ILqEHA.3068@TK2MSFTNGP15.phx.gbl ?????g...
> 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/<O8Y7D1KqEHA.2588@TK2MSFTNGP12.phx.gbl>
> those static methods like GetLoginUser, DataTools.EscapeMask are just for > string editing, and do not relate to the sql objects [quoted text clipped - 9 lines] > 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 [quoted text clipped - 6 lines] > > > > 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, [quoted text clipped - 4 lines] > > Richard Blewett - DevelopMentor > > http://staff.develop.com/richardb/weblog nntp://news.microsoft.com/microsoft.public.dotnet.framework/<#KKSDiGqEHA.3668@TK2MSFTNGP15.phx.gbl>
> > This is my assembly object's method: > > [quoted text clipped - 47 lines] > > > Richard Blewett - DevelopMentor > > > http://staff.develop.com/richardb/weblog nntp://news.microsoft.com/microsoft.public.dotnet.framework/<ud$vTSFqEHA.2456@TK2MSFTNGP10.phx.gbl>
> > > 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? [quoted text clipped - 20 lines] > > [microsoft.public.dotnet.framework]
[microsoft.public.dotnet.framework]
Nick Malik - 03 Oct 2004 07:52 GMT 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
> This is my assembly object's method: > [quoted text clipped - 42 lines] > > Richard Blewett - DevelopMentor > > http://staff.develop.com/richardb/weblog nntp://news.microsoft.com/microsoft.public.dotnet.framework/<ud$vTSFqEHA.2456@TK2MSFTNGP10.phx.gbl>
> > Can i pass a SqlConnection object with opened connection to a > > method/function as a parameter? [quoted text clipped - 19 lines] > > > > [microsoft.public.dotnet.framework]
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|