473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Shared Sub

Can someone please tell me why as soon as I put the following sub into a user
control and then make the sub shared it stops working???

It works fine if I put the code directly into the aspx page... ???

I think it has something to do with:

"Cannot refer to an instance of a class within a shared method or shared
member initializer without an explicit instance of the class"

If I declare the objects in the shared sub the user control runs but the
value of ImageFile returns as nothing...

Can someone please give some suggestions to what I might be doing wrong..

Thanks

CODE::...

Public Shared Sub UploadData()
'Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
Dim imgstream As Stream = ImageFile.PostedFile.InputStream
Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
imgstream.Read(imgdata, 0, ImageFile.PostedFile.ContentLength)
Dim MyConn As New
SqlConnection(ConfigurationSettings.AppSettings("s trConn"))
Dim cmd As New SqlCommand("fileUpload", MyConn)
cmd.CommandType = CommandType.StoredProcedure

Dim titleparam As New SqlParameter("@fileTitle", SqlDbType.NVarChar,
255)
Dim descriptionparam As New SqlParameter("@fileDescription",
SqlDbType.NVarChar, 255)
Dim Ownerparam As New SqlParameter("@fileOwner", SqlDbType.NVarChar,
255)
Dim Officeparam As New SqlParameter("@officeID", SqlDbType.Int)
Dim typeparam As New SqlParameter("@fileType", SqlDbType.NVarChar,
100)
Dim dataparam As New SqlParameter("@fileData", SqlDbType.Image)

'Dim ImageTitle As System.Web.UI.WebControls.TextBox
'Dim fldOwner As System.Web.UI.WebControls.TextBox
'Dim fldDescription As System.Web.UI.WebControls.TextBox
'Dim ddlOffice As System.Web.UI.WebControls.DropDownList

titleparam.Value = ImageTitle.Text
descriptionparam.Value = fldDescription.Text
Ownerparam.Value = fldOwner.Text
Officeparam.Value = ddlOffice.SelectedIndex
typeparam.Value = ImageFile.PostedFile.ContentType
dataparam.Value = imgdata

cmd.Parameters.Add(titleparam)
cmd.Parameters.Add(descriptionparam)
cmd.Parameters.Add(Ownerparam)
cmd.Parameters.Add(Officeparam)
cmd.Parameters.Add(typeparam)
cmd.Parameters.Add(dataparam)

MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
End Sub
Nov 19 '05 #1
3 2956
Tim:

I don't see why you would want to make the Sub shared. You want to
have the method associated with an instance of your object, so remove
the shared keyword.

When you mark a method as Shared, you can't touch any of the instance
data in the class (like ImageFile), because that method does not act
on an instance of your user control.

When you mark a field as Shared (like ImageFile), you've said that
there is only one ImageFile for all the UserControl instances that
will ever be created in your application. That's certainly not what
you want.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Wed, 18 May 2005 06:18:04 -0700, "Tim::.." <myatix_at_hotmail.com>
wrote:
Can someone please tell me why as soon as I put the following sub into a user
control and then make the sub shared it stops working???

It works fine if I put the code directly into the aspx page... ???

I think it has something to do with:

"Cannot refer to an instance of a class within a shared method or shared
member initializer without an explicit instance of the class"

If I declare the objects in the shared sub the user control runs but the
value of ImageFile returns as nothing...

Can someone please give some suggestions to what I might be doing wrong..

Thanks

CODE::...

Public Shared Sub UploadData()
'Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
Dim imgstream As Stream = ImageFile.PostedFile.InputStream
Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
imgstream.Read(imgdata, 0, ImageFile.PostedFile.ContentLength)
Dim MyConn As New
SqlConnection(ConfigurationSettings.AppSettings(" strConn"))
Dim cmd As New SqlCommand("fileUpload", MyConn)
cmd.CommandType = CommandType.StoredProcedure

Dim titleparam As New SqlParameter("@fileTitle", SqlDbType.NVarChar,
255)
Dim descriptionparam As New SqlParameter("@fileDescription",
SqlDbType.NVarChar, 255)
Dim Ownerparam As New SqlParameter("@fileOwner", SqlDbType.NVarChar,
255)
Dim Officeparam As New SqlParameter("@officeID", SqlDbType.Int)
Dim typeparam As New SqlParameter("@fileType", SqlDbType.NVarChar,
100)
Dim dataparam As New SqlParameter("@fileData", SqlDbType.Image)

'Dim ImageTitle As System.Web.UI.WebControls.TextBox
'Dim fldOwner As System.Web.UI.WebControls.TextBox
'Dim fldDescription As System.Web.UI.WebControls.TextBox
'Dim ddlOffice As System.Web.UI.WebControls.DropDownList

titleparam.Value = ImageTitle.Text
descriptionparam.Value = fldDescription.Text
Ownerparam.Value = fldOwner.Text
Officeparam.Value = ddlOffice.SelectedIndex
typeparam.Value = ImageFile.PostedFile.ContentType
dataparam.Value = imgdata

cmd.Parameters.Add(titleparam)
cmd.Parameters.Add(descriptionparam)
cmd.Parameters.Add(Ownerparam)
cmd.Parameters.Add(Officeparam)
cmd.Parameters.Add(typeparam)
cmd.Parameters.Add(dataparam)

MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
End Sub


Nov 19 '05 #2
OK... But when I try and refer to the sub in my aspx file I get the following
issue!

Reference to a non shared member requires a n object reference...

What does this mean and how do I fix it???

Thanks for the reply!

"Scott Allen" wrote:
Tim:

I don't see why you would want to make the Sub shared. You want to
have the method associated with an instance of your object, so remove
the shared keyword.

When you mark a method as Shared, you can't touch any of the instance
data in the class (like ImageFile), because that method does not act
on an instance of your user control.

When you mark a field as Shared (like ImageFile), you've said that
there is only one ImageFile for all the UserControl instances that
will ever be created in your application. That's certainly not what
you want.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Wed, 18 May 2005 06:18:04 -0700, "Tim::.." <myatix_at_hotmail.com>
wrote:
Can someone please tell me why as soon as I put the following sub into a user
control and then make the sub shared it stops working???

It works fine if I put the code directly into the aspx page... ???

I think it has something to do with:

"Cannot refer to an instance of a class within a shared method or shared
member initializer without an explicit instance of the class"

If I declare the objects in the shared sub the user control runs but the
value of ImageFile returns as nothing...

Can someone please give some suggestions to what I might be doing wrong..

Thanks

CODE::...

Public Shared Sub UploadData()
'Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
Dim imgstream As Stream = ImageFile.PostedFile.InputStream
Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
imgstream.Read(imgdata, 0, ImageFile.PostedFile.ContentLength)
Dim MyConn As New
SqlConnection(ConfigurationSettings.AppSettings(" strConn"))
Dim cmd As New SqlCommand("fileUpload", MyConn)
cmd.CommandType = CommandType.StoredProcedure

Dim titleparam As New SqlParameter("@fileTitle", SqlDbType.NVarChar,
255)
Dim descriptionparam As New SqlParameter("@fileDescription",
SqlDbType.NVarChar, 255)
Dim Ownerparam As New SqlParameter("@fileOwner", SqlDbType.NVarChar,
255)
Dim Officeparam As New SqlParameter("@officeID", SqlDbType.Int)
Dim typeparam As New SqlParameter("@fileType", SqlDbType.NVarChar,
100)
Dim dataparam As New SqlParameter("@fileData", SqlDbType.Image)

'Dim ImageTitle As System.Web.UI.WebControls.TextBox
'Dim fldOwner As System.Web.UI.WebControls.TextBox
'Dim fldDescription As System.Web.UI.WebControls.TextBox
'Dim ddlOffice As System.Web.UI.WebControls.DropDownList

titleparam.Value = ImageTitle.Text
descriptionparam.Value = fldDescription.Text
Ownerparam.Value = fldOwner.Text
Officeparam.Value = ddlOffice.SelectedIndex
typeparam.Value = ImageFile.PostedFile.ContentType
dataparam.Value = imgdata

cmd.Parameters.Add(titleparam)
cmd.Parameters.Add(descriptionparam)
cmd.Parameters.Add(Ownerparam)
cmd.Parameters.Add(Officeparam)
cmd.Parameters.Add(typeparam)
cmd.Parameters.Add(dataparam)

MyConn.Open()
cmd.ExecuteNonQuery()
MyConn.Close()
End Sub


Nov 19 '05 #3
Could you show the code you are using and explain what you are trying
to do?

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Wed, 18 May 2005 09:46:03 -0700, "Tim::.." <myatix_at_hotmail.com>
wrote:
OK... But when I try and refer to the sub in my aspx file I get the following
issue!

Reference to a non shared member requires a n object reference...

What does this mean and how do I fix it???

Thanks for the reply!

"Scott Allen" wrote:
Tim:

I don't see why you would want to make the Sub shared. You want to
have the method associated with an instance of your object, so remove
the shared keyword.

When you mark a method as Shared, you can't touch any of the instance
data in the class (like ImageFile), because that method does not act
on an instance of your user control.

When you mark a field as Shared (like ImageFile), you've said that
there is only one ImageFile for all the UserControl instances that
will ever be created in your application. That's certainly not what
you want.

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Wed, 18 May 2005 06:18:04 -0700, "Tim::.." <myatix_at_hotmail.com>
wrote:
>Can someone please tell me why as soon as I put the following sub into a user
>control and then make the sub shared it stops working???
>
>It works fine if I put the code directly into the aspx page... ???
>
>I think it has something to do with:
>
>"Cannot refer to an instance of a class within a shared method or shared
>member initializer without an explicit instance of the class"
>
>If I declare the objects in the shared sub the user control runs but the
>value of ImageFile returns as nothing...
>
>Can someone please give some suggestions to what I might be doing wrong..
>
>Thanks
>
>CODE::...
>
>Public Shared Sub UploadData()
> 'Dim ImageFile As System.Web.UI.HtmlControls.HtmlInputFile
> Dim imgstream As Stream = ImageFile.PostedFile.InputStream
> Dim imgdata(ImageFile.PostedFile.ContentLength) As Byte
> imgstream.Read(imgdata, 0, ImageFile.PostedFile.ContentLength)
>
>
> Dim MyConn As New
>SqlConnection(ConfigurationSettings.AppSettings(" strConn"))
> Dim cmd As New SqlCommand("fileUpload", MyConn)
> cmd.CommandType = CommandType.StoredProcedure
>
> Dim titleparam As New SqlParameter("@fileTitle", SqlDbType.NVarChar,
>255)
> Dim descriptionparam As New SqlParameter("@fileDescription",
>SqlDbType.NVarChar, 255)
> Dim Ownerparam As New SqlParameter("@fileOwner", SqlDbType.NVarChar,
>255)
> Dim Officeparam As New SqlParameter("@officeID", SqlDbType.Int)
> Dim typeparam As New SqlParameter("@fileType", SqlDbType.NVarChar,
>100)
> Dim dataparam As New SqlParameter("@fileData", SqlDbType.Image)
>
> 'Dim ImageTitle As System.Web.UI.WebControls.TextBox
> 'Dim fldOwner As System.Web.UI.WebControls.TextBox
> 'Dim fldDescription As System.Web.UI.WebControls.TextBox
> 'Dim ddlOffice As System.Web.UI.WebControls.DropDownList
>
> titleparam.Value = ImageTitle.Text
> descriptionparam.Value = fldDescription.Text
> Ownerparam.Value = fldOwner.Text
> Officeparam.Value = ddlOffice.SelectedIndex
> typeparam.Value = ImageFile.PostedFile.ContentType
> dataparam.Value = imgdata
>
> cmd.Parameters.Add(titleparam)
> cmd.Parameters.Add(descriptionparam)
> cmd.Parameters.Add(Ownerparam)
> cmd.Parameters.Add(Officeparam)
> cmd.Parameters.Add(typeparam)
> cmd.Parameters.Add(dataparam)
>
> MyConn.Open()
> cmd.ExecuteNonQuery()
> MyConn.Close()
> End Sub



Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
96
by: BadPony | last post by:
Anyone using Peoplesoft on a Federated UDB (shared nothing)Environment on Open System Platforms? Preferably AIX, but any war stories would be good. TEA EB-C
10
by: darrel | last post by:
I'm still trying to sort out in my head the differences between public and shared when referring to declaring properties or variables. This is my understanding: shared - akin to a 'global'...
1
by: Henri | last post by:
Hi, I'm using a custom class in my ASP.NET which has a Shared property. I don't want this Shared property to be shared between users because of thread concurrent accesses problems. I just want it...
2
by: tshad | last post by:
I have a program I am trying to compile into a dll and am getting a bunch of: the following errors: error BC30469: Reference to a non-shared member requires an object reference. At first, I...
11
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
15
by: Rob Nicholson | last post by:
A consequence of the ASP.NET architecture on IIS has just hit home with a big thud. It's to do with shared variables. Consider a module like this: Public Module Functions Public GlobalName As...
5
by: Erik Cruz | last post by:
Hello! I have read some threads discussing the fact that a module is in reality a shared class. If I try to create a Public Shared Class in vb.net I receive a compile error. Why? If I can't...
5
by: Simon | last post by:
Hi all, We have an ASP.NET 1.1 application running on IIS6 on Server 2003. Most of the base objects we are using in this application are taken from a windows application also written by us. We...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.