hi i tried to display image from sql table to gridview but its not displaying this s my code to bind gridview with sql table records..
sql.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblsession.Text = "Welcome" + Convert.ToString(Session["UName"]);
sessionimage();
}
}
private void sessionimage()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connect"].ConnectionString);
try
{
string UName = (string)Session["UName"];
SqlDataAdapter Adp = new SqlDataAdapter("select Photo from TBL_PBLogin where UName='"+ UName +"'", con);
DataTable Dt = new DataTable();
con.Open();
Adp.Fill(Dt);
gridviewphoto.DataSource = Dt;
gridviewphoto.DataBind();
con.Close();
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
sql.aspx

Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 20, 2015, 4:19 AM EDT
You are almost there. Just include the EmpID in your query:
SqlDataAdapter Adp = new SqlDataAdapter("select EmpID,Photo from TBL_PBLogin where EmpID='" + EmpID + "'", con);
Dipankar BiswasRank #992 · 2K reputationPosted May 19, 2015, 4:43 AM EDT
{
string useremail = (string)Session["useremail"];
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select user_profile_pic from tbl_user where user_email='" + useremail + "'";
sqlda = new SqlDataAdapter(str, con);
dt = new DataTable();
sqlda.Fill(dt);
sqlda.Dispose();
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
Deepak Ratan175 reputationPosted May 19, 2015, 4:53 AM EDT
Dipankar BiswasRank #992 · 2K reputationPosted May 19, 2015, 5:18 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 19, 2015, 5:26 AM EDT
Deepak Ratan175 reputationPosted May 19, 2015, 5:27 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 19, 2015, 5:30 AM EDT
Deepak Ratan175 reputationPosted May 19, 2015, 5:35 AM EDT
Deepak Ratan175 reputationPosted May 19, 2015, 5:40 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 19, 2015, 5:42 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 19, 2015, 5:45 AM EDT
Dipankar BiswasRank #992 · 2K reputationPosted May 19, 2015, 5:48 AM EDT
Deepak Ratan175 reputationPosted May 19, 2015, 5:59 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 19, 2015, 6:01 AM EDT
Deepak Ratan175 reputationPosted May 19, 2015, 6:02 AM EDT
Deepak Ratan175 reputationPosted May 19, 2015, 6:05 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 19, 2015, 6:10 AM EDT
Dipankar BiswasRank #992 · 2K reputationPosted May 19, 2015, 6:14 AM EDT
{
if (up_load_new_img.PostedFile.ContentLength > 0)
{
s1 = Path.GetFileName(up_load_new_img.FileName);
path = Server.MapPath("file_upload") + "/" + s1;
up_load_new_img.SaveAs(path);
}
SqlConnection ronicon = new SqlConnection(strConnString);
ronicon.Open();
SqlCommand ronicom = new SqlCommand("sp_up_img", ronicon);
ronicom.CommandType = CommandType.StoredProcedure;
ronicom.Connection = ronicon;
ronicom.Parameters.AddWithValue("@usr_name", lb1.Text);
ronicom.Parameters.AddWithValue("@usr_upload_image", s1);
ronicom.ExecuteNonQuery();
ronicom.Dispose();
save_and_show();
ronicon.Close();
}
Deepak Ratan175 reputationPosted May 19, 2015, 6:14 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 19, 2015, 6:17 AM EDT
Deepak Ratan175 reputationPosted May 19, 2015, 9:40 PM EDT
rajshree soni7 reputationPosted May 20, 2015, 1:15 AM EDT
Upendra Pratap ShahiRank #329 · 7.5K reputationPosted May 20, 2015, 2:41 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 20, 2015, 3:48 AM EDT
You need to pass the ID to your handler. It should look like this:
<asp:Image ID="Image" runat="server" ImageUrl='<%# "Handler.ashx?id=" + Eval("Id") %>' />
Deepak Ratan175 reputationPosted May 20, 2015, 3:54 AM EDT
Deepak Ratan175 reputationPosted May 20, 2015, 4:05 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 20, 2015, 4:17 AM EDT
The error is pretty much self-explanatory. That means the Id field isn't in your select query. You need to replace that with the unique identifier that holds your Images. In other words replace the ID with the primary key of your table where your image is stored.
So for example if you have this table:
Deepak Ratan175 reputationPosted May 20, 2015, 4:36 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 20, 2015, 4:46 AM EDT
Where did you put your Handler? based on your code your Handler file should be in the same location where your page is located.
Deepak Ratan175 reputationPosted May 20, 2015, 4:54 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 20, 2015, 5:15 AM EDT
And when you debug the ProcessRequest() method never get hit?
Can you try referencing the path like this:
Deepak Ratan175 reputationPosted May 20, 2015, 5:24 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 20, 2015, 5:49 AM EDT
Deepak Ratan175 reputationPosted May 20, 2015, 5:54 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 20, 2015, 6:00 AM EDT
Deepak Ratan175 reputationPosted May 20, 2015, 6:04 AM EDT
Deepak Ratan175 reputationPosted May 20, 2015, 6:14 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 20, 2015, 6:29 AM EDT
Deepak Ratan175 reputationPosted May 20, 2015, 6:41 AM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 20, 2015, 7:45 AM EDT
Deepak Ratan175 reputationPosted May 20, 2015, 8:55 PM EDT
Nanhe SiddiqueRank #1,103 · 1.7K reputationPosted May 27, 2015, 7:41 PM EDT
S Sreelakshmi74 reputationPosted May 31, 2015, 10:52 PM EDT
Vincent Maverick DuranoRank #238 · 10.3K reputationPosted May 31, 2015, 11:37 PM EDT
That wont work for his case as he is storing the image as binary in the database. Please try to read the entire post again.
Thanks,
Vinz
Carlos kambuiRank #2,642 · 528 reputationPosted Jun 12, 2015, 12:22 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts