Shared Sub

G

Guest

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
 
S

Scott Allen

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.
 
G

Guest

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top