传递一个NULL值一个GUID [英] Passing a NULL value to a GUID
问题描述
我在VB.NET共初学者,所以你可能需要我来承担,但我需要背后编辑这个code,这样一个空值传递给我的数据库为图片网址字段。前端是一个Web表单,用户可以输入一书的细节,以及上传一本书的封面的选项。
我想改变我的code,以便在文件上传对话框不履行hasFile,在数据库中生成的将是一个NULL值的GUID字符串(这是这样我就可以有一个无可用的图像的形象使用ASP的NullImageUrl属性。)
这是我试过到目前为止实现,但智能感知告诉我,String类型的值不能转换为'的System.Guid。
code背后:
进口System.Data.OleDb
部分公共类addBook
继承System.Web.UI.Page保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load结束小组保护小组btn_submission_Click(BYVAL发件人为对象,BYVAL E上EventArgs的)把手btn_submission.Click
NOFILE暗淡作为字符串=没有
昏暗myGUID = Guid.NewGuid()
昏暗newFileName作为字符串= myGUID.ToString()及.JPG
昏暗fileLocationOnServerHardDisk = Request.MapPath(IMG /拇指)及/&放大器; newFileName
如果fu_picture.HasFile然后
fu_picture.SaveAs(fileLocationOnServerHardDisk)
其他
myGUID = NOFILE
万一
昏暗oleDbConn作为新OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings(BookMeetConnString)。ConnectionString中)
昏暗的SqlString作为字符串=INSERT INTO书目(标题,作者,PublicationDate,页数,出版社,Blurb的,imgUrl的,AverageRating)
值(@ F1,F2 @,@ F3,F4 @,@ F5,F6 @,@ F7,F8 @)
昏暗CMD作为OleDbCommand的=新的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 /拇指/&放大器; newFileName)
cmd.Parameters.AddWithValue(@ F8,rbl_Stars.SelectedValue)
oleDbConn.Open()
cmd.ExecuteNonQuery()
System.Threading.Thread.Sleep(2000)
的Response.Redirect(〜/ addedrecord.aspx)结束小组保护小组rbl_Stars_SelectedIndexChanged(BYVAL发件人为对象,BYVAL E上EventArgs的)把手rbl_Stars.SelectedIndexChanged结束小组
末级
块引用>请告诉我,如果我在我的思路完全错误的!
编辑:在这个present时刻,即使文件没有上传,在数据库表中生成一个GUID字符串+ JPG后缀即使图像本身不存在
解决方案您应该通过的 DBNull.Value 您的数据库,如果你不要求
cmd.Parameters.AddWithValue(@ F7,_
如果(fu_picture.HasFile,IMG /拇指/&放大器; newFileName,DbNull.Value)的<一个href=\"http://stackoverflow.com/questions/576431/is-there-a-conditional-ternary-operator-in-vb-net\">ternary运营商允许在创建参数你只是测试的标志HasFile。结果
如果是假的,你设置的参数值DBNull.Value。如果HasFile是真的,你可以建立正确的路径到你的镜像文件。当然,这消除了需要分配没有之前的code至myGuid。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
解决方案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.这篇关于传递一个NULL值一个GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!