I have an issue trying to display image in my DataList from the database and have access to the database that I'm pulling my images from. But when I run the WebForm the image does not appear, what is wrong in my codes?
<asp:DataList ID="DataList1" runat="server" DataKeyField="dishID" DataSourceID="SqlDataSource1" BorderStyle="Solid" GridLines="Both" RepeatColumns="4" RepeatDirection="Horizontal" Width="1259px" >
<ItemTemplate>
<table class="auto-style1">
<tr><td>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "FoodPictures/" + Eval("dishImage") %>' />
This is how I'm retrieving from the database:
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(_connStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Dish";
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
conn.Close();
DataList1.DataSource = dt;
DataList1.DataBind();
conn.Close();
}
I think Your Page do not get image on proper path....
use
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "../FoodPictures/" + Eval("dishImage") %>' />
Or
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/FoodPictures/" + Eval("dishImage") %>' />
Try this..
Assuming if dishImage is a name like imagename.jpg stored in database then try Convert.ToString(Eval("dishImage")) like:
<asp:Image ID="Image1" ImageUrl='<% # "~/FoodPictures/" + Convert.ToString(Eval("dishImage")) %>' runat="server" />
Related
My C# Code
public partial class Message : System.Web.UI.Page
{
String strconn = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
members();
}
public void members()
{
SqlConnection con = new SqlConnection(strconn);
con.Open();
try
{
string str = "Select Users.Username,Users.Name,ProfilePic.Pathh From Users FULL OUTER JOIN ProfilePic ON Users.username = ProfilePic.Username ORDER BY Users.Sno";
SqlCommand cmd = new SqlCommand(str,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
repMembers.DataSource = ds;
repMembers.DataBind();
GridView1.DataSource = ds;
GridView1.DataBind();
cmd.Dispose();
con.Close();
}
catch(Exception ex) {
lbMembers.Text = ex.ToString();
}
}
}
And My HTML Code
<asp:Repeater ID="repMembers" runat="server" >
<asp:ItemTemplate >
<div style="width:100%; border-bottom:#ffffff 2px solid;"> <asp:Image ID="Image4" runat="server" ImageUrl='<%#Eval("Pathh") %>' width="60px" Height="60px"/> <asp:Label ID="lb" runat="server" Text='<%#Eval("Username") %>'></asp:Label><br/><asp:Label ID="lbname" runat="server" Text='<%#Eval("Name") %>'></asp:Label><br/></div>
</asp:ItemTemplate>
</asp:Repeater>
My Table is absolutely correct and is working, i checked it with Grid View. It gives me the "Name","Username", and "Path" Stored.
I checked it with SQL query and displayed it in grid view.
This is the Photo of my table,it is the combination of two tables
Use ItemTemplate instead of asp:ItemTemplate. There's also a typo with the word "Pathh", does this solve your issue?
<ItemTemplate >
<div style="width:100%; border-bottom:#ffffff 2px solid;">
<asp:Image ID="Image4" runat="server" ImageUrl='<%#Eval("Path") %>' width="60px" Height="60px"/>
<asp:Label ID="lb" runat="server" Text='<%#Eval("Username") %>'></asp:Label>
<br/>
<asp:Label ID="lbname" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
<br/>
</div>
</ItemTemplate>
The part of the code am working on is vulnerable to stored XSS. Below is the code.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_OnRowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" Width ="1000px" class="grid">
<Columns>
<asp:TemplateField HeaderText="User Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Uname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("Uname") %>'></asp:TextBox> //this is the line vulnerable to XSS
</EditItemTemplate>
</asp:TemplateField> </columns>
</asp:GridView>
code behind
DataTable dt = new DataTable();
try
{
SqlConnection con = new SqlConnection(conn);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter("Select Uid,Uname,Utype,Uemail,ClientName,ProjectName,Ulog from usrtable where ClientName='" + clientname + "' and Utype='Admin' or ClientName='" + clientname + "'and Utype='Normal'", con);
adapt.Fill(dt);
con.Close();
}
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
Can you let me know where am going wrong. Is it on the client side where I am binding the column names to textbox in gridview?
This question already exists:
how to show data in gridview from arraylist in asp.net?
Closed 8 years ago.
I have a database where there is userid, problemname and status column. I am retrieving this data from database in an ArrayList and returning it. Now to show in GridView I have taken a DataTable and in the DataTable I have put three columns and I just want to show my data that is saved in the ArrayList in these columns by making one row.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
ArrayList myArrayList = ConvertDataSetToArrayList();
// Display each item of ArrayList
DataTable dt = new DataTable();
dt.Columns.Add("User Id");
dt.Columns.Add("Problem Name");
dt.Columns.Add("Status");
foreach (Object row in myArrayList)
{
dt.Rows.Add();
dt.Rows[dt.Rows.Count - 1]["User Id"] = ((DataRow)row)["userid"].ToString();
dt.Rows[dt.Rows.Count - 1]["Problem Name"] = ((DataRow)row) ["problemname"].ToString();
dt.Rows[dt.Rows.Count - 1]["Status"] = ((DataRow)row)["status"].ToString();
}
GridView1.DataSource =dt;
GridView1.DataBind();
}
public ArrayList ConvertDataSetToArrayList()
{
string con = " ";
con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection objsqlconn = new SqlConnection(con);
objsqlconn.Open();
SqlCommand cmd = new SqlCommand("SELECT userid,problemname,status FROM problemtable", objsqlconn);
cmd.ExecuteNonQuery();
cmd.CommandType = CommandType.Text;
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = cmd;
DataSet myDataSet = new DataSet();
myAdapter.Fill(myDataSet);
ArrayList myArrayList = new ArrayList();
foreach (DataRow dtRow in myDataSet.Tables[0].Rows)
{
myArrayList.Add(dtRow);
}
objsqlconn.Close();
return myArrayList;
}
Here is my html:
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="cdd">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
Why is my data is not showing in my GridView?
You need to bind your data to specific controls in your gridview. For example, you need to have labels in your gridview itemtemplate to bind your data to. Here is an example:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="cdd">
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Id">
<ItemTemplate>
<asp:Label ID="lbl_userid" runat="server" Text='<%# Eval("User Id") %>' CssClass="lbl"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Problem Name">
<ItemTemplate>
<asp:Label ID="lbl_problemname" runat="server" Text='<%# Eval("Problem Name") %>' CssClass="lbl"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="status">
<ItemTemplate>
<asp:Label ID="lbl_status" runat="server" Text='<%# Eval("Status") %>' CssClass="lbl"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I agree that an ArrayList is a poor choice, but, regardless of how you're binding the data, you need to tell the gridview what it's supposed to show. You can do this using inline tags or by using an onitemdatabound trigger. I recommend you look up some more examples of gridviews.
I'm using a Datalist to show countries list each record having a Gridview containing a list of states and each state having a Dropdownlist for cities. I am using a hidden field to get the ID of country to retrieve the name of state for every record by using Eval("C_ID") in the hidden field. But it raises ArgumentOutOfRangeException. I'm not able to know what I'm doing wrong.
Here is the code for Datalist:
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" onitemdatabound="DataList1_ItemDataBound" >
<ItemTemplate>
<table border="1px">
<tr>
<td>
<%#Eval("C_Name") %><br />
<asp:HiddenField ID="HiddenField1" Value='<%#Eval("C_ID") %>' runat="server" />
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="State Cities">
<ItemTemplate >
<asp:CheckBox ID="CheckBox1" runat="server" />
<%#Eval("S_Name") %>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
and this is code behind:
public partial class AdvancedTable : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from Country", con);
con.Open();
da.Fill(ds, "Country");
con.Close();
DataList1.DataSource = ds;
DataList1.DataBind();
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
SqlDataAdapter da2 = new SqlDataAdapter();
da2.SelectCommand = new SqlCommand("select * from State where C_ID='"+((HiddenField)DataList1.Items[e.Item.ItemIndex].FindControl("HiddenField1")).Value+"'",con);
con.Open();
da2.Fill(ds, "State");
con.Close();
((GridView)e.Item.FindControl("GridView1")).DataSource = ds.Tables["State"];
((GridView)e.Item.FindControl("GridView1")).DataBind();
}
}
Perhaps, instead of DataList1.Items[e.Item.ItemIndex].FindControl("HiddenField1")
Just use e.Item.FindControl("HiddenField1")
Instead, you can do the following:
<asp:DropDownList ID="DropDownList1" runat="server" DataSource="<%# GetList((int)Eval("C_ID"))%>">
</asp:DropDownList>
In the code Behind
protected DataTable GetList(int id){ }
I have the next code:
<asp:ListView ID="moreI"
runat="server"
DataSourceID="LinqDataSource1" onprerender="moreI_PreRender">
<ItemTemplate>
<asp:Image ID="Image1" Width="100px" Height="100px" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' />
// and so on till the </ItemTemplate> and </asp:ListView>
I have the next method:
protected void checkTheImage()
{
foreach (ListViewItem item in moreI.Items)
{
((Image)item.FindControl("Image1")).ImageUrl = "noImage.jpg";
}
}
And the sql
protected Boolean ImageCheck()
{
SqlConnection connection = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\***.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
string CommandText2 = "SELECT noImage FROM Machreta WHERE noImage = 1";
SqlCommand command2 = new SqlCommand(CommandText2, connection);
connection.Open();
Boolean check = (Boolean)command2.ExecuteScalar();
connection.Close();
//here i stoped without the return.
I have the table Machreta with the column noImage (bit).
where the noImage = true, i want to show the noImage.jpg from the checkTheImage()
where the noImage = false, i want to show the '<%# Eval("ImageUrl") %>'
I have problem with the logic, maybe too many hours of work... what can you suggest me?
I think you need to update the datasource that bind to the listview to contain the boolean variable that specify if to use the ImageUrl or the noimage.jpeg
<asp:ListView ID="moreI"
runat="server"
DataSourceID="LinqDataSource1" onprerender="moreI_PreRender">
<ItemTemplate>
<asp:Image ID="Image1" Width="100px" Height="100px" runat="server" ImageUrl='<%# checkImage(Eval("ImageUrl"), Eval("noImage")) %>' />
// and so on till the </ItemTemplate> and </asp:ListView>
protected string checkTheImage(object ImageUrl, object noImage)
{
if((bool)noImage)
{
return "noImage.jpg";
}
else
{
return ImageUrl.ToString();
}
}