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 7 1875
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
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? 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
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
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
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?
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Akbar |
last post by:
Hey there,
Big-time curiosity issue here...
Here's the test code (it's not that long)... it's to display a large
number of image links with captions, ideally pulled in from an
external file...
|
by: Patrick.O.Ige |
last post by:
When i try and use this (Where Unit is a column in my Table):-
If Unit Is DBNull.Value Then
Return "1"
Else
Return "2"
End If
I always have 2 returned! Even when Unit is NULL!
I want a...
|
by: Tina |
last post by:
I have instantiated an insertRow for a dataset. I now want to make the
GLCode DBNull.
I have tried:
insertRow.GLCode = Convert.dbnull
insertRow.GLCode = Convert.dbnull(insertRow.GLCode)
and...
|
by: DavidS |
last post by:
I have HTML web page with <asp:Image id=img_L runat=server ImageAlign=Top
Visible=True Width=y Height=x></asp:Image>. For some images, less than 128K
I can view image. Other image files > 128K,...
|
by: Bob |
last post by:
I'm sure there's a good reason, I just haven't been able to think of it -
why didn't MS allow "DBNull" values to simply be a null (Nothing)?
Bob
|
by: thehuby |
last post by:
I am building a CMS and as part of it a user can upload an image.
Once uploaded I am displaying the image.
If the user then decides they want to replace the image with another I
get a caching...
|
by: gunimpi |
last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431
********************************************************
VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help
wanted...
|
by: xla76 |
last post by:
I have a function that returns an array of string values, occasionally
the returned array value is 'Nothing'
I need to check if the value is nothing before moving on - how can I do
this - none of...
|
by: trint |
last post by:
Some of the selections of the GridView1 do not have images and they
return a DBNull value. I have tried ways to handle that and one leads
to
another problem. Do you know how to handle this error...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |