Re: Can i pass a SqlConnection object with opened connection to a method/function as a parameter?
From: Parco (parco_at_nowlover.com)
Date: 10/02/04
- Next message: Jon Skeet [C# MVP]: "Re: Adding the .NET Framework in an Application"
- Previous message: John Cosmas: "Re: Client Side Values passed from Server"
- In reply to: Richard Blewett [DevelopMentor]: "Re: Can i pass a SqlConnection object with opened connection to a method/function as a parameter?"
- Next in thread: Richard Blewett [DevelopMentor]: "Re: Can i pass a SqlConnection object with opened connection to a method/function as a parameter?"
- Reply: Richard Blewett [DevelopMentor]: "Re: Can i pass a SqlConnection object with opened connection to a method/function as a parameter?"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 3 Oct 2004 02:25:56 +0800
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 ...)
>
> 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/
> >
> > 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]
Relevant Pages
... But the post was about the String and StringBuilder classes, ... > Sherif ElMetainy wrote: ... >> static methods that manipulates strings. ... >> Best regards, ...
(microsoft.public.dotnet.languages.csharp)
... Regards ... those static methods like GetLoginUser, ... GetLoginUser, DataTools.EscapeMask or that the stored procedure doesn't ... possibility of a SQL injection attack by using string concatenation ...
(microsoft.public.dotnet.framework)
... I believe the File and Directory objects have static methods to parse the ... string into its components. ... "Lauchlan M" wrote in message ... > What I want to do is get the filename and the ...
(microsoft.public.dotnet.framework.aspnet)
... You should create a "helper" class with static methods. ... name all these classes XxxHelper, ... Create a class inherited from string and use that for my strings, ...
(microsoft.public.dotnet.languages.csharp)
... methods in your long concatenated string, ... DataTools.EscapeMask method (a static method, apparently), or the ... > connection and what you do with it when you get it ... >> the existed opened sqlconnection object to do it and pass the object to ...
(microsoft.public.dotnet.framework)