By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,262 Members | 2,664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,262 IT Pros & Developers. It's quick & easy.

Help with DBNull issue when displaying images

P: n/a
I have a website that has articles and images. The articles are stored
in one table, and if they have an image associated with them, it's
stored in another table with a common id linking them. I can pull all
the information fine into labels, and if the ImagePath field is NULL,
then it just doesn't show up. My problem is that when I'm trying to
display the images, I get a "Conversion from type 'DBNull' to type
'String' is not valid" error when that field is NULL. How can I get
this to work??

This is my code on the aspx page to display the image:

<img src='\main\content\images\<%#
System.IO.Path.GetFileName(Eval("ImagePath"))%>' width="150px">

This is my code on the aspx.vb page to pull the image:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim connStr As String = "Data Source=server;Initial
Catalog=database; User ID=user;Password=password;"

Dim connObj As SqlClient.SqlConnection = New
SqlClient.SqlConnection

Dim connCmd As New SqlClient.SqlCommand

Dim strID As String
strID = Request.QueryString("cidL")

Dim selectstr As String = "Select ImagePath from Image where
cid='" + strID + "'"

Dim dataread As SqlClient.SqlDataReader

Dim unameexists As Boolean

connObj.ConnectionString = connStr

connObj.Open()

connCmd.CommandText = selectstr

connCmd.Connection = connObj

dataread = connCmd.ExecuteReader()

DataList1.DataBind()

dataread.Close()

End Sub

Apr 19 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Well, since you aren't creating proper tiers, you could simply change ur
query to something like:

"SELECT IsNull(ImagePath, "na.gif") AS ImagePath FROM Image...
also, ur not doing urself any favor by not using try/finally and not using
command paramters. Your code is open to a huge (and easily exploitable) SQL
Injection attack. If your connection string is using the SA account, I'd
have ur database formatted in about 2 seconds...after I got the data off of
it.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
<mo*******@hotmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
I have a website that has articles and images. The articles are stored
in one table, and if they have an image associated with them, it's
stored in another table with a common id linking them. I can pull all
the information fine into labels, and if the ImagePath field is NULL,
then it just doesn't show up. My problem is that when I'm trying to
display the images, I get a "Conversion from type 'DBNull' to type
'String' is not valid" error when that field is NULL. How can I get
this to work??

This is my code on the aspx page to display the image:

<img src='\main\content\images\<%#
System.IO.Path.GetFileName(Eval("ImagePath"))%>' width="150px">

This is my code on the aspx.vb page to pull the image:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim connStr As String = "Data Source=server;Initial
Catalog=database; User ID=user;Password=password;"

Dim connObj As SqlClient.SqlConnection = New
SqlClient.SqlConnection

Dim connCmd As New SqlClient.SqlCommand

Dim strID As String
strID = Request.QueryString("cidL")

Dim selectstr As String = "Select ImagePath from Image where
cid='" + strID + "'"

Dim dataread As SqlClient.SqlDataReader

Dim unameexists As Boolean

connObj.ConnectionString = connStr

connObj.Open()

connCmd.CommandText = selectstr

connCmd.Connection = connObj

dataread = connCmd.ExecuteReader()

DataList1.DataBind()

dataread.Close()

End Sub

Apr 19 '06 #2

P: n/a
Well, I just switched from using ASP to ASP.NET last week, so I'm still
fairly new to all of this and have been using the tools in VS2005. I
have tried using the Select IsNull.....but it didn't work. Also, what
should I do with the Try/Finally you mentioned and command parameters?

Apr 19 '06 #3

P: n/a
mo*******@hotmail.com wrote:
Well, I just switched from using ASP to ASP.NET last week, so I'm still
fairly new to all of this and have been using the tools in VS2005. I
have tried using the Select IsNull.....but it didn't work. Also, what
should I do with the Try/Finally you mentioned and command parameters?


exception handling, etc (there are so many articles that could go over
how to properly code error handling, btw, so feel free to google for
more, they're out there):
http://www.dotnetjohn.com/articles.aspx?articleid=42

parameterized queries are what you're looking for regarding your inline SQL:
http://www.4guysfromrolla.com/webtech/092601-1.shtml

hth
--
Craig
Microsoft MVP - ASP/ASP.NET
Apr 19 '06 #4

P: n/a
Do a google search for "SafeDataReader".

Looks like you're using VB.net , and most versions of it are in vb.net

http://www.lhotka.net/Articles.aspx?...3-8b5bda6bad22
A couple of things.

At the very least, you ought to move your code to an object, which takes
your strID as a parameter.... and returns an IDataReader (or a
SafeDataReader if you choose that route)

I don't know if you're code is production or example ....it its production,
then I'd try to clean it up a little, by encapsulating the logic somewhere.

Once you get a SafeDataReader back.... then you can use it.

You might also write a little wrapper function on the aspx code behind page

public function CheckForFileExists ( imgName as string) as string
if imgName.Length > 0 then
return imgName
else
return ""
end if
end function

where I have return imgName, you could build your html code to show the
image.


<mo*******@hotmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
I have a website that has articles and images. The articles are stored
in one table, and if they have an image associated with them, it's
stored in another table with a common id linking them. I can pull all
the information fine into labels, and if the ImagePath field is NULL,
then it just doesn't show up. My problem is that when I'm trying to
display the images, I get a "Conversion from type 'DBNull' to type
'String' is not valid" error when that field is NULL. How can I get
this to work??

This is my code on the aspx page to display the image:

<img src='\main\content\images\<%#
System.IO.Path.GetFileName(Eval("ImagePath"))%>' width="150px">

This is my code on the aspx.vb page to pull the image:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim connStr As String = "Data Source=server;Initial
Catalog=database; User ID=user;Password=password;"

Dim connObj As SqlClient.SqlConnection = New
SqlClient.SqlConnection

Dim connCmd As New SqlClient.SqlCommand

Dim strID As String
strID = Request.QueryString("cidL")

Dim selectstr As String = "Select ImagePath from Image where
cid='" + strID + "'"

Dim dataread As SqlClient.SqlDataReader

Dim unameexists As Boolean

connObj.ConnectionString = connStr

connObj.Open()

connCmd.CommandText = selectstr

connCmd.Connection = connObj

dataread = connCmd.ExecuteReader()

DataList1.DataBind()

dataread.Close()

End Sub

Apr 19 '06 #5

P: n/a
You can also use the MS Data access application block. as DAL.
http://www.microsoft.com/downloads/d...displaylang=en

It is part of the Enterprise Library now:
FOR 2.0 :
http://msdn.microsoft.com/library/?u...ml/EntLib2.asp

http://www.microsoft.com/downloads/d...displaylang=en

FOR 1.1 :
http://www.microsoft.com/downloads/d...displaylang=en
"sloan" <sl***@ipass.net> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Do a google search for "SafeDataReader".

Looks like you're using VB.net , and most versions of it are in vb.net

http://www.lhotka.net/Articles.aspx?...3-8b5bda6bad22
A couple of things.

At the very least, you ought to move your code to an object, which takes
your strID as a parameter.... and returns an IDataReader (or a
SafeDataReader if you choose that route)

I don't know if you're code is production or example ....it its
production,
then I'd try to clean it up a little, by encapsulating the logic
somewhere.

Once you get a SafeDataReader back.... then you can use it.

You might also write a little wrapper function on the aspx code behind
page

public function CheckForFileExists ( imgName as string) as string
if imgName.Length > 0 then
return imgName
else
return ""
end if
end function

where I have return imgName, you could build your html code to show the
image.


<mo*******@hotmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
I have a website that has articles and images. The articles are stored
in one table, and if they have an image associated with them, it's
stored in another table with a common id linking them. I can pull all
the information fine into labels, and if the ImagePath field is NULL,
then it just doesn't show up. My problem is that when I'm trying to
display the images, I get a "Conversion from type 'DBNull' to type
'String' is not valid" error when that field is NULL. How can I get
this to work??

This is my code on the aspx page to display the image:

<img src='\main\content\images\<%#
System.IO.Path.GetFileName(Eval("ImagePath"))%>' width="150px">

This is my code on the aspx.vb page to pull the image:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim connStr As String = "Data Source=server;Initial
Catalog=database; User ID=user;Password=password;"

Dim connObj As SqlClient.SqlConnection = New
SqlClient.SqlConnection

Dim connCmd As New SqlClient.SqlCommand

Dim strID As String
strID = Request.QueryString("cidL")

Dim selectstr As String = "Select ImagePath from Image where
cid='" + strID + "'"

Dim dataread As SqlClient.SqlDataReader

Dim unameexists As Boolean

connObj.ConnectionString = connStr

connObj.Open()

connCmd.CommandText = selectstr

connCmd.Connection = connObj

dataread = connCmd.ExecuteReader()

DataList1.DataBind()

dataread.Close()

End Sub


Apr 19 '06 #6

P: n/a
Dim connectionString As String = "Data Source=server;Initial
Catalog=database; User ID=user;Password=password;"
dim connection as SqlConnection
dim command as SqlCommand
dim reader as SqlDataReader
try
connection = new SqlConnection(connectionString)
command = new SqlCommand()
command.CommandText = "SELECT ImagePath from Image where cid =
@CategoryId"
command.Parameters.Add("@CategoryId", SqlDbType.VarChar, 5).Value =
categoryId;
command.Connection = connection
connection.Open()
reader = command.ExecuteReader()
DataList1.DataSource = reader;
DataList1.DataBind()
finally
if not connection is nothing then
connection.Dispose()
end if
if not command is nothing then
command.Dispose()
end if
if not reader is nothing then
reader.Dispose()
end if
end try

--
http://www.openmymind.net/
http://www.fuelindustries.com/
<mo*******@hotmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Well, I just switched from using ASP to ASP.NET last week, so I'm still
fairly new to all of this and have been using the tools in VS2005. I
have tried using the Select IsNull.....but it didn't work. Also, what
should I do with the Try/Finally you mentioned and command parameters?

Apr 19 '06 #7

P: n/a
Ok, so I put that code in the apsx.vb right? So where do I put what the
@Categoryid is? Do I call the Function on the aspx page?

Apr 20 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.