Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi i have three file upload to store path image in database.
i want when i insert two of three pic in database (like below pic)

http://8pic.ir/images/98875708231203613698.jpg[^]

don't show null image figure in my datalist (like below pic)
http://8pic.ir/images/84846050397371303282.jpg[^]
I only want data list don't display any thing if image file path is null in the db...please help
Thanks
Posted
Updated 10-May-14 19:10pm
v3
Comments
Darshan.Pa 10-May-14 6:57am    
can you put your code.
sajad379 10-May-14 10:45am    
in file upload section:
protected void Button1_Click(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(FileUpload2.FileName))
{
FileUpload2.SaveAs("" + Server.MapPath("..//") + "\\images/htcpic/\\" + FileUpload2.FileName);
}
if(!string.IsNullOrEmpty(FileUpload3.FileName))
{
FileUpload3.SaveAs("" + Server.MapPath("..//") + "\\images/htcpic/\\" + FileUpload3.FileName);
}
if (!string.IsNullOrEmpty(FileUpload4.FileName))
{
FileUpload4.SaveAs("" + Server.MapPath("..//") + "\\images/htcpic/\\" + FileUpload4.FileName);
}
string sajad = "Data Source=.;Initial Catalog=minus;Integrated Security=True";
SqlConnection conn = new SqlConnection(sajad);
SqlCommand sd = new SqlCommand("insert into mspc(title,image2,image3,image4) values (@title,@image2,@image3,@image4)", conn);
sd.Parameters.AddWithValue("@image", FileUpload1.FileName);
sd.Parameters.AddWithValue("@image2", FileUpload2.FileName);
sd.Parameters.AddWithValue("@image3", FileUpload3.FileName);
sd.Parameters.AddWithValue("@image4", FileUpload4.FileName);
sd.Parameters.AddWithValue("@title", TextBox1.Text);
conn.Open();
sd.ExecuteNonQuery();
conn.Close(); }
}
----------------------
in datalist:
<asp:DataList ID="DataList2" runat="server" DataKeyField="id" DataSourceID="SqlDataSource1" RepeatDirection="Horizontal">
<itemtemplate>
<br />

<img alt='<%# Eval("title") %>' height="178" src='/images/htcpic/<%#Eval("image2") %>' style="border-right: #cccccc thin solid; border-top: #cccccc thin solid; border-left: #cccccc thin solid; border-bottom: #cccccc thin solid"
width="120" />



<img alt='<%# Eval("title") %>' height="178" src='/images/htcpic/<%#Eval("image3") %>' style="border-right: #cccccc thin solid; border-top: #cccccc thin solid; border-left: #cccccc thin solid; border-bottom: #cccccc thin solid"
width="120" />



<img alt='<%# Eval("title") %>' height="178" src='/images/htcpic/<%#Eval("image4") %>' style="border-right: #cccccc thin solid; border-top: #cccccc thin solid; border-left: #cccccc thin solid; border-bottom: #cccccc thin solid"
width="120" />



Nelek 10-May-14 17:41pm    
If you have to add code, then do it in the question using "improve question" widget please.

You can add the format and use the code tags, which improve readability for the rest of us

1 solution

You should use asp:Image[^] control instead of plain html img tag, like this:
ASP.NET
<asp:image runat="server" AlternateText='<%# Eval("title")%>' ImageUrl='/images/htcpic/<%# Eval("image4")%>' Visible='<%# !String.IsNullOrEmpty(Convert.ToString(Eval("image4")))%>' Height="178" Width="120" />
 
Share this answer
 
v3
Comments
sajad379 11-May-14 7:02am    
I have error message in this line:
visible="<%# !String.IsNullOrEmpty(Eval("image4"))%>"
-------
Compilation Error
Compiler Error Message: CS1502: The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments
-------
and in asp error list:
error1:Argument 1: cannot convert from 'object' to 'string'
error2:The best overloaded method match for 'string.IsNullOrEmpty(string)' has some invalid arguments
Kornfeld Eliyahu Peter 11-May-14 7:04am    
Wrap Eval in Convert.ToString()...
sajad379 11-May-14 8:40am    
How I do it?please give me example like Solution 3...tnx
Kornfeld Eliyahu Peter 11-May-14 8:42am    
See updated solution...
sajad379 11-May-14 9:55am    
sorry but by this code anything not show in outpute...<br>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900