1

I am a total beginner at VB.NET so you may need to bear with me but I need to edit this code behind so that a null value is passed to my database for the 'imageurl' field. The front end is a web form where a user can enter details of a book, with the option of uploading a book cover.

I want to change my code so that if the file upload dialog does not fulfil hasFile, the GUID string generated in the database will be a NULL value (this is so I can have a 'no image available' image using the NullImageUrl property in ASP.)

This is what I've tried to implement so far, but intellisense is telling me that "Value of type String cannot be converted to 'System.GUID'.

Code Behind:

Imports System.Data.OleDb 
Partial Public Class addBook
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Protected Sub btn_submission_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submission.Click
    Dim noFile As String = Nothing
    Dim myGUID = Guid.NewGuid()
    Dim newFileName As String = myGUID.ToString() & ".jpg"
    Dim fileLocationOnServerHardDisk = Request.MapPath("img/thumb") & "/" & newFileName
    If fu_picture.HasFile Then
        fu_picture.SaveAs(fileLocationOnServerHardDisk)
    Else
        myGUID = noFile
    End If
    Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
    Dim SqlString As String = "Insert into booklist(Title,Author,PublicationDate,Pages,Publisher,Blurb,imgurl,AverageRating)
Values (@f1,@f2,@f3,@f4,@f5,@f6,@f7,@f8)"
    Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
    cmd.CommandType = CommandType.Text
    cmd.Parameters.AddWithValue("@f1", tb_booktitle.Text)
    cmd.Parameters.AddWithValue("@f2", tb_bookauthor.Text)
    cmd.Parameters.AddWithValue("@f3", tb_bookpubyear.Text)
    cmd.Parameters.AddWithValue("@f4", tb_bookpages.Text)
    cmd.Parameters.AddWithValue("@f5", tb_publisher.Text)
    cmd.Parameters.AddWithValue("@f6", tb_blurb.Text)
    cmd.Parameters.AddWithValue("@f7", "img/thumb/" & newFileName)
    cmd.Parameters.AddWithValue("@f8", rbl_Stars.SelectedValue)
    oleDbConn.Open()
    cmd.ExecuteNonQuery()
    System.Threading.Thread.Sleep("2000")
    Response.Redirect("~/addedrecord.aspx")

End Sub

Protected Sub rbl_Stars_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbl_Stars.SelectedIndexChanged

End Sub 
End Class

Please tell me if I'm completely wrong in my line of thinking!

EDIT: At this present moment, even if a file is not uploaded, a guid string + jpg suffix are generated in the database table even if the image itself doesn't exist

adaam
  • 3,700
  • 7
  • 27
  • 51

1 Answers1

3

You should pass DBNull.Value to your db if you fail the requirement

 cmd.Parameters.AddWithValue("@f7", _
               if(fu_picture.HasFile, "img/thumb/" & newFileName, DbNull.Value)

The ternary operator allows you to test the flag HasFile just when you create the parameter.
If it is false you set the parameter value to DBNull.Value. If HasFile is true you can build the correct path to your imagefile. Of course this removes the necessity to assign Nothing to myGuid in the code before.

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286