views:

277

answers:

1

I've got a simple upload form. Here's my code:

<form id="Form1" method="post" enctype="multipart/form-data" runat="server"
    <asp:label id="lblMsg" runat="server" CssClass="msg" />
    <span class="msg">Select Gallery:</span>
    <asp:listbox id="gallerySelect" runat="server" Rows="1" DataTextField="galleryName" DataValueField="galleryID" />
    <span class="msg">File:</span><input type="file" id="galleryLogo" name="galleryLogo" runat="server" />
    <input type="button" value="Upload" OnServerClick="Upload" runat="server"><br />
</form>

You are supposed to be able to select which gallery you want to upload a logo image for, and then select your image file and click the upload button. Here's my upload sub:

Sub Upload(Source As Object, e As EventArgs)
    If Not (galleryLogo.PostedFile Is Nothing) Then
        Dim intFileNameLength as Integer
        Dim strFileNamePath as String
        Dim strFileNameOnly as String
        'Logic to find the FileName (excluding the path)
        strFileNamePath = galleryLogo.PostedFile.FileName
        intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
        strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
        galleryLogo.PostedFile.SaveAs("c:\inetpub\wwwroot\galleries\" & strFileNameOnly)

        Dim cmd As SqlCommand
        Cache("sqlconn") = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
        Cache("sqlconn").Open()
        Dim query = "update gallery set logo = '" & strFileNameOnly & "' where gallery_id = " & gallerySelect.SelectedItem.Value.ToString()
        cmd = New SqlCommand(query, Cache("sqlconn"))
        cmd.ExecuteNonQuery()   
        tell the user something positive
        lblMsg.Text = "File Uploaded Successfully<br />"
        End If
End Sub

My problem is that the data bound listbox gallerySelect's selectedIndex is always -1. I know its because it's data bound but I don't know what I have to do to be able to get the value. Any help would be appreciated.

EDIT:

Here's my binding code if you need it:

Dim galleryCmdSelect2 As SqlCommand
Dim galleryData2 As SqlDataReader
galleryCmdSelect2 = New SqlCommand("select gallery_name as galleryName, gallery_id as galleryID from galleries order by gallery_name", Cache("sqlconn"))
galleryData2 = galleryCmdSelect2.ExecuteReader()
gallerySelect.DataSource = galleryData2
gallerySelect.DataBind()
galleryData2.Close()
+1  A: 

If you're databinding the control each postback, this will clear the SelectedValue, instead wrap your databind code like this:

If Not (IsPostBack) Then
 //DataBind here
End If
Nick Craver
I added this code, and then received an "object reference not set to an instance of an object" error when trying to access the listbox items.
shady
@shady - Something else going on, need to see your binding order, can you post your code-behind so we can see what's happening?
Nick Craver
I posted the code that I used to bind the listbox. It's all in one aspx file, I'm not compiling I'm on a remote server with dreamweaver. Don't have visual studio.
shady
@shady - I don't see the wrapper in there, can you post the entire thing? If DataSource is getting set without being populated it'll cause the list of items to be empty as well (DataBind to a null basically). I have to run but will check in on this a bit later.
Nick Craver