Thursday, 21 August 2014

Store images to DB and bind to GridView in ASP.NET C#


This will show you how to store images to database and bind to gridView. I have used FileUpload control to upload image from computer and it will be stored to database as binary format and display these images to GridView.  
          First we have to create a table with three fields like  ImageId, ImageName and Image. We need to store Image as binary format so we have to use varbinary() datatype for the column Image. 
            Then we design our ASPX page. First we have add TextBox control for image name, FileUpload control for image and Button to store data in DB, Gridview for Display Image.
<table>
        <tr>
            <td>
                ImagrName:
            </td>
            <td>
                <asp:TextBox ID="txtImageName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Browse Image:
            </td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
    <asp:Button ID="btnSubmit"runat="server" Text="Submit" OnClick="btnSubmit_Click" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
        </tr>
        <tr>
            <td colspan="2">
              <asp:GridView ID="gvwImageDetails" runat="server" AutoGenerateColumns="false">
              <Columns>
              <asp:TemplateField HeaderText="Image Name">
              <ItemTemplate>
              <asp:Label ID="Label1" runat="server" Text='<%#Eval("ImageName")%>'></asp:Label>
              </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText="Image">
              <ItemTemplate>
              <asp:Image ID="imgPreview" ImageUrl='<%#"ShowImage.ashx?ImageId="+ Eval("ImageId") %>' runat="server" Height="120px" Width="100px" />
              </ItemTemplate>
              </asp:TemplateField>
              </Columns>
              </asp:GridView>
            </td>
        </tr>
    </table>
On button click event we have to write following code to store image to database.
  
protected void btnSubmit_Click(object sender, EventArgs e)
    {   
     try
     {
         Byte[] bImage = null;
         if (FileUpload1.HasFile)
         {
             string filename = FileUpload1.PostedFile.FileName;
             string filePath = Path.GetFileName(filename);
             Stream fs = FileUpload1.PostedFile.InputStream;
             BinaryReader br = new BinaryReader(fs);
             bImage = br.ReadBytes((Int32)fs.Length);

             string strQuery = "insert into Images (ImageName,Image) values (@ImgName,@Img)";

             string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConString"].ConnectionString;
             using (SqlConnection con = new SqlConnection(strConnString))
              {                    
                con.Open();
                SqlCommand cmd = new SqlCommand(strQuery, con);
                cmd.Parameters.Add("@ImgName", SqlDbType.NVarChar, 50).Value = txtImageName.Text;
                cmd.Parameters.Add("@Img", SqlDbType.Binary).Value = bImage;
                cmd.ExecuteNonQuery();
                con.Close();
                }
            }
            BindGrid();
        }
        catch (Exception ex)
        {
        }
}

Bind GridView Code
private void BindGrid()
 {
   DataSet ds = new DataSet();
   string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLConString"].ConnectionString;
   using (SqlConnection con = new SqlConnection(strConnString))
   {
      con.Open();
      string cmdstring = "Select distinct ImageId,ImageName,Image from Images";
      SqlCommand cmd = new SqlCommand(cmdstring, con);
      SqlDataAdapter adp = new SqlDataAdapter(cmd);
      adp.Fill(ds);

      gvwImageDetails.DataSource = ds;
      gvwImageDetails.DataBind();
    }
  }

Then Add new generic handler in the Project, here ShowImage.ashx is my generic handler.
Now we have Handler file containing "ProcessRequest(HttpContext context)" method which gets triggered automatically.

public void ProcessRequest(HttpContext context)
    {
       SqlDataReader dr = null;        SqlConnection conn = null;       
SqlCommand selectcmd = null;
        try
        {
         conn = new SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings["SQLConString"].ConnectionString);
         selectcmd = new SqlCommand("select Image from Images where ImageId=" + context.Request.QueryString["ImageId"], conn);
         conn.Open();
         dr = selectcmd.ExecuteReader();
         while (dr.Read())
         {
             context.Response.ContentType = "image/jpg";
             context.Response.BinaryWrite((byte[])dr["Image"]);
         }
         dr.Close();
        }
        finally
        {
            conn.Close();
        }
    }
 
Now run the Project The result is Like given below. 



 

 
 
 

This tutorial will show you how to store images to database and bind to gridView. I have used FileUpload control to upload image from our computer and it’ll be stored to database as binary format and display these images to GridView.  
          First we have to create a table with three fields, ImageID, ImageName and Image. We need to store Image as binary format so we have to use varbinary() datatype.
- See more at: http://www.dotnetfox.com/articles/store-images-to-database-and-bind-to-gridview-in-Asp-Net-1114.aspx#sthash.He3NynJk.dpuf
This tutorial will show you how to store images to database and bind to gridView. I have used FileUpload control to upload image from our computer and it’ll be stored to database as binary format and display these images to GridView.  
          First we have to create a table with three fields, ImageID, ImageName and Image. We need to store Image as binary format so we have to use varbinary() datatype.
- See more at: http://www.dotnetfox.com/articles/store-images-to-database-and-bind-to-gridview-in-Asp-Net-1114.aspx#sthash.He3NynJk.dpuf

Monday, 23 June 2014

how to add tooltip for button in c# winforms



There is a one simple function written below. If you wont please use it in your Page load Function.

ToolTip tooltip1 = new System.Windows.Forms.ToolTip();
tooltip1.SetToolTip(btnVendorReset, "Refresh");

convert first letter to Uppercase using CSS



CSS may do what you wish:


<style type="text/css">
        .UpperCaseFirstLetter
        {
            text-transform: capitalize;
        }
 </style>


<asp:TextBox ID="TextBox2" runat="server"
        Width="200px" CssClass="UpperCaseFirstLetter"></asp:TextBox>
 

How to show label in blinking style in WinForms



If you want to show label in blinking style in WinForms Please use the given below Query.

private void Test_Load(object sender, EventArgs e) //PageLoad
{
    timer1.Interval = 250;
    timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
    this.label1.Visible = !this.label1.Visible;
}



Thank You.