title: c# asp.net displaying image in gridview
body: <p>I wish to display an image in a gridview.</p>
  
  <p>I have a database with 4 columns - id, author, comment, filename (which holds the img path)</p>
  
  <p><a href="http://postimg.org/image/4loz8t9fb/" rel="nofollow noreferrer">http://postimg.org/image/4loz8t9fb/</a></p>
  
  <p>I have an image upload method so when the user writes his comment, chooses a photo and clicks submit- his name, the comment and the photo path goes to the database (and the photo itself is uploaded to 'Images' folder in the project folder).</p>
  
  <pre><code>&lt;asp:GridView ID="GridView2" runat="server" AutoGenerateColumns = "False"
                     Font-Names = "Arial" Height="375px" Width="498px" DataSourceID="SqlDataSource2" &gt;
                  &lt;Columns&gt;
  
                      &lt;asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author" /&gt;
                      &lt;asp:BoundField DataField="Comment" HeaderText="Comment" SortExpression="Comment" /&gt;
                      &lt;asp:BoundField DataField="FileName" HeaderText="FileName" SortExpression="FileName" /&gt;
                      &lt;asp:TemplateField HeaderText="Image"&gt;
  
                        &lt;ItemTemplate &gt;
  
                          &lt;asp:Image ID="Image1" runat="server" ImageUrl ='&lt;%# Eval("FileName") %&gt;' height="120px" Width="150px" /&gt;
  
                        &lt;/ItemTemplate&gt;
  
                      &lt;/asp:TemplateField&gt;
                  &lt;/Columns&gt;
                  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>here is the c# code-</p>
  
  <pre><code>protected void addcomment_Click(object sender, EventArgs e)
      {
          string FileName = "";
          if (fileupload.PostedFile != null)
          {
               FileName = Path.GetFileName(fileupload.PostedFile.FileName);
               //Save files to disk
               fileupload.SaveAs(Server.MapPath("Images/" + FileName));
  
          }
  
  
  
  
          if (TextBox1.Text.Length &gt; 0)
          {
              string name = firstName + " " + lastName;
              SqlCommand cmd = new SqlCommand("insert into comments values(@name,@comment,@filename)", conn);
              cmd.Parameters.AddWithValue("@name", name);
              cmd.Parameters.AddWithValue("@comment", TextBox1.Text);
              cmd.Parameters.AddWithValue("@filename", Server.MapPath("Images/" + FileName));
              conn.Open();
              cmd.ExecuteNonQuery();
              GridView1.DataBind();
              conn.Close();
              TextBox1.Text = "";
              Response.Redirect(Request.Url.AbsoluteUri);
          }
          else
          {
              TextBox1.Text = "You have to type something buddy!";
          }
  
      }
  </code></pre>
  
  <p>The gridview datasource is the sql of course.</p>
  
  <p>Here is an image of the database and the website</p>
  
  <p><a href="http://postimg.org/image/4loz8t9fb/" rel="nofollow noreferrer">http://postimg.org/image/4loz8t9fb/</a></p>