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\cont ent\images\<%#
System.IO.Path. GetFileName(Eva l("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.EventArg s) Handles Me.Load
Dim connStr As String = "Data Source=server;I nitial
Catalog=databas e; User ID=user;Passwor d=password;"
Dim connObj As SqlClient.SqlCo nnection = New
SqlClient.SqlCo nnection
Dim connCmd As New SqlClient.SqlCo mmand
Dim strID As String
strID = Request.QuerySt ring("cidL")
Dim selectstr As String = "Select ImagePath from Image where
cid='" + strID + "'"
Dim dataread As SqlClient.SqlDa taReader
Dim unameexists As Boolean
connObj.Connect ionString = connStr
connObj.Open()
connCmd.Command Text = selectstr
connCmd.Connect ion = connObj
dataread = connCmd.Execute Reader()
DataList1.DataB ind()
dataread.Close( )
End Sub 7 1888
Well, since you aren't creating proper tiers, you could simply change ur
query to something like:
"SELECT IsNull(ImagePat h, "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*******@hotm ail.com> wrote in message
news:11******** *************@g 10g2000cwb.goog legroups.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\cont ent\images\<%# System.IO.Path. GetFileName(Eva l("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.EventArg s) Handles Me.Load Dim connStr As String = "Data Source=server;I nitial Catalog=databas e; User ID=user;Passwor d=password;"
Dim connObj As SqlClient.SqlCo nnection = New SqlClient.SqlCo nnection
Dim connCmd As New SqlClient.SqlCo mmand
Dim strID As String strID = Request.QuerySt ring("cidL")
Dim selectstr As String = "Select ImagePath from Image where cid='" + strID + "'"
Dim dataread As SqlClient.SqlDa taReader
Dim unameexists As Boolean
connObj.Connect ionString = connStr
connObj.Open()
connCmd.Command Text = selectstr
connCmd.Connect ion = connObj
dataread = connCmd.Execute Reader()
DataList1.DataB ind()
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*******@hotma il.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 CheckForFileExi sts ( 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*******@hotm ail.com> wrote in message
news:11******** *************@g 10g2000cwb.goog legroups.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\cont ent\images\<%# System.IO.Path. GetFileName(Eva l("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.EventArg s) Handles Me.Load Dim connStr As String = "Data Source=server;I nitial Catalog=databas e; User ID=user;Passwor d=password;"
Dim connObj As SqlClient.SqlCo nnection = New SqlClient.SqlCo nnection
Dim connCmd As New SqlClient.SqlCo mmand
Dim strID As String strID = Request.QuerySt ring("cidL")
Dim selectstr As String = "Select ImagePath from Image where cid='" + strID + "'"
Dim dataread As SqlClient.SqlDa taReader
Dim unameexists As Boolean
connObj.Connect ionString = connStr
connObj.Open()
connCmd.Command Text = selectstr
connCmd.Connect ion = connObj
dataread = connCmd.Execute Reader()
DataList1.DataB ind()
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.ne t> wrote in message
news:%2******** ********@TK2MSF TNGP02.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 CheckForFileExi sts ( 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*******@hotm ail.com> wrote in message news:11******** *************@g 10g2000cwb.goog legroups.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\cont ent\images\<%# System.IO.Path. GetFileName(Eva l("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.EventArg s) Handles Me.Load Dim connStr As String = "Data Source=server;I nitial Catalog=databas e; User ID=user;Passwor d=password;"
Dim connObj As SqlClient.SqlCo nnection = New SqlClient.SqlCo nnection
Dim connCmd As New SqlClient.SqlCo mmand
Dim strID As String strID = Request.QuerySt ring("cidL")
Dim selectstr As String = "Select ImagePath from Image where cid='" + strID + "'"
Dim dataread As SqlClient.SqlDa taReader
Dim unameexists As Boolean
connObj.Connect ionString = connStr
connObj.Open()
connCmd.Command Text = selectstr
connCmd.Connect ion = connObj
dataread = connCmd.Execute Reader()
DataList1.DataB ind()
dataread.Close( )
End Sub
Dim connectionStrin g As String = "Data Source=server;I nitial
Catalog=databas e; User ID=user;Passwor d=password;"
dim connection as SqlConnection
dim command as SqlCommand
dim reader as SqlDataReader
try
connection = new SqlConnection(c onnectionString )
command = new SqlCommand()
command.Command Text = "SELECT ImagePath from Image where cid =
@CategoryId"
command.Paramet ers.Add("@Categ oryId", SqlDbType.VarCh ar, 5).Value =
categoryId;
command.Connect ion = connection
connection.Open ()
reader = command.Execute Reader()
DataList1.DataS ource = reader;
DataList1.DataB ind()
finally
if not connection is nothing then
connection.Disp ose()
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*******@hotm ail.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.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 (that part's not here -- spotlighting the problem code):
--------BEGIN CODE PAGE------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
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 case if a column is NULL the it xhould return '1 and if not '2'
How does DBNull.Value work?
|
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 several other things.
How is it done?
|
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, image is not viewable - comes up
with X in picture box. Is there size restriction. How can I remove this -
or how can I show images > 128K in web page.
NOTE: x & y in asp:Image spec are distinct values - say 128 x 128 or 64 x 64
image sizes
|
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 issue in the browser as the image has the same name.
Without having to use unique file names each time the user replaces an
image, how can I force the browser to check for the file properly?
|
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
********************************************************
For this teeny job, please refer to:
http://feeds.reddit.com/feed/8fu/?o=25
|
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 the options i've looked at seems to fit? (vb2005)
Thanks
Pete
|
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 message if an
image path column returns a NULL?
Here is the code:
protected
string DetectImagePath(object filename)
{
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |