Wednesday 18 January 2012

How to Insert and Retreive Checkbox List Item in Database


'How to Insert

Dim Selectval As String = ""
Dim I As Integer

For I = 0 To ChkListCallOrder.Items.Count - 1
      If ChkListCallOrder.Items(I).Selected = True Then
Selectval = Selectval + ChkListCallOrder.Items(I).Value & "#"
      End If
      Next

'Note:- Pass the parameter in you Query or Stored Procedure

cmd.Parameters.AddWithValue("@Call_Order", Selectval)

'How To Reterive and Bind
ChkListCallOrder.ClearSelection()If Not IsDBNull(ds.Tables("ClientTable").Rows(0).Item("Call_Order")) Then
chkboxlst = Split(ds.Tables(
Dim chkboxlst As Array"ClientTable").Rows(0).Item("Call_Order"), "#")Dim I As IntegerIf chkboxlst.Length > 0 Then
ChkListCallOrder.Items.FindByValue(chkboxlst(I)).Selected =
For I = 0 To chkboxlst.Length - 2TrueNextEnd IfEnd If


Tuesday 17 January 2012

How to Make Floating Menu Or Cart on Your Browser Screen?

Some Times we needs some contents on the screen when we scroll down our window screen. For that we can create Floating Div. Given below steps Explain that how can we implement.


1. Put The Given Below Script Inside <Head> tag.




     <script type="text/javascript"> 
     floatingMenu.add('floatdiv'
         { 
             // Represents distance from left or right browser window 
             // border depending upon property used. Only one should be 
             // specified. 
             // targetLeft: 0, 
             targetRight: 10, 
   
             // Represents distance from top or bottom browser window 
             // border depending upon property used. Only one should be 
             // specified. 
             targetTop: 10, 
             // targetBottom: 0, 
  
             // Uncomment one of those if you need centering on 
             // X- or Y- axis. 
             // centerX: true, 
             // centerY: true, 
  
             // Remove this one if you don't want snap effect 
             snap: true 
         });


2. Download .js File From Given Below Link.




3. Add a Folder Name with Name Javascript in Your Project Directory. And Save Given above link File in this Directory.


4. Add the Given Link File in your .Aspx Page <head> tag as Given Below:


<script type="text/javascript" src="Javascript/FloatDiv.js"> </script>

5. Finally Add the Given Below <Div> inside your <Body> Tag.

<div id="floatdiv" style="position:absolute;width:300px;height:50px;top:3px;right:0px;background:#FFFFFF;border:1px solid #2266AA;z-index:100"> 

 Your Floating Div is here.

</div> 













How to Bind Data List With Product Images From Database




How to Bind With Data List:



<asp:DataList ID="DlProducts" runat="server" Height="138px" Width="416px" Font-Names="Verdana" Font-Size="Small">
   

 <ItemTemplate>
    <div style="border:1px solid black;" id ="divProd" runat=server>
        <table style="width: 550px">
        <tr align =left>
            <td rowspan="4" style="width: 126px;border:0px;" align =left>
                <asp:Label ID="LblProdId" runat ="server" CssClass="LabelStyle" Text='<%# DataBinder.Eval(Container.DataItem, "Product_id") %>' Width="200px" Font-Bold =true Visible=false ></asp:Label>
                <asp:Image ID="Img" runat="server" Height="60px" ImageUrl='<%# "Picture.aspx?id=" & Eval("Product_id")%>' Width="70px" />
               
                </td>
            <td style="width: 224px">
                        <asp:Label ID="ProdName" runat ="server" CssClass="LabelStyle" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' Width="200px" Font-Bold =true></asp:Label></td>
            <td style="width: 102px">
                            <asp:Label ID="LblQty" runat="server" Font-Names="Verdana" Font-Size="Small" Text="Quantity" Visible=false></asp:Label>
                            <asp:Label ID="LblWeight" runat="server" Font-Names="Verdana" Font-Size="Small" Text="Select Weight"></asp:Label>
                            </td>
            <td>
                            <asp:TextBox ID="txtqty" runat="server" Width="64px" Height=14px Text = "1" style="text-align:right;" OnTextChanged="txtqty_TextChanged" AutoPostBack="True" Visible =false></asp:TextBox>
                            <asp:DropDownList ID="DdlWeight" runat="server" Width = 70px Height = 19px  AutoPostBack ="true" OnSelectedIndexChanged ="DdlWeight_SelectedIndexChanged"
                               
                                DataSource = '<% #DdlBindWeight(Eval("Product_id"))%>' DataValueField = "data_id" DataTextField = "data"
                                Font-Size="12px" CssClass = "SetIndex"></asp:DropDownList>
                            </td>
        </tr>
        <tr>
            <td style="width: 224px">
                        <asp:Label ID="lblAmount" runat ="server" CssClass="LabelStyle" Text='<%# DataBinder.Eval(Container.DataItem, "Sales_Price") %>' Width="200px"></asp:Label>
                        <asp:Label ID="CalAmt" runat ="server" CssClass="LabelStyle" Text='<%# DataBinder.Eval(Container.DataItem, "Price") %>' Width="200px" Visible =false></asp:Label>
                        </td>
            <td style="width: 102px">
            </td>


        </tr>
        <tr>
            <td style="width: 224px; height: 21px">
                        <asp:Label ID="Label3" runat ="server" CssClass="LabelStyle" Text='<%# DataBinder.Eval(Container.DataItem, "Description") %>' Width="200px"></asp:Label></td>
            <td style="width: 102px; height: 21px">
            </td>
            <td style="height: 21px">
            </td>
        </tr>
        <tr>
            <td style="width: 224px">
            </td>
            <td style="width: 102px">
            </td>
            <td>
            </td>
        </tr>
    </table>             
          
</div>
 </ItemTemplate>
       
</asp:DataList>

Sunday 8 January 2012

How to Store and Display Image in Image Control from Database

Some Times happens that we tries to Show in Image in Box using Image controls. Developers use different Kinds of Strategies like to store the Photos in Folder and Load Photo from this Folder. But It does not works Sometimes. The better way is to store image in database in Binary Format and show it on web page. Now How can we do it given below Steps:


Step 1: Upload and Insert Image into database Table



protected void BtnUpload_Click(object sender, EventArgs e)
    {
        System.Drawing.Image imag = System.Drawing.Image.FromStream(
            FileUpload1.PostedFile.InputStream);
        System.Data.SqlClient.SqlConnection conn = null;
        try
        {
            try
            {
                conn = new System.Data.SqlClient.SqlConnection(@"server=ADIMPS-DEL-075\SQLEXPRESS;trusted_connection = yes;database = sample;");
                conn.Open();
                System.Data.SqlClient.SqlCommand insertCommand = new System.Data.SqlClient.SqlCommand("Insert into [emp](id,name,city,Head_id,Emp_Image) Values (008,'Nishchal','Gzb','9',@Pic)", conn);
                insertCommand.Parameters.Add("@Pic", SqlDbType.Image, 0).Value = ConvertImgToByteArr(imag, System.Drawing.Imaging.ImageFormat.Jpeg);
                int queryResult = insertCommand.ExecuteNonQuery();
                if (queryResult == 1)
                   LblMsg.Text = "msg record Created Successfully";
            }
            catch (Exception ex)
            {
                LblMsg.Text = "Error: " + ex.Message;
            }
        }
        finally
        {
            if (conn != null)
                conn.Close();
        }
        ShowDataList();
    }
 





// function to Convert Image to Byte Arrary//
private byte[] ConvertImgToByteArr(System.Drawing.Image imageToConvert,System.Drawing.Imaging.ImageFormat formatOfImage)
    {
        byte[] ByteValue;
        try
        {
            using (MemoryStream ms = new MemoryStream())
            {
                imageToConvert.Save(ms, formatOfImage);
                ByteValue = ms.ToArray();
            }
        }
        catch (Exception) { throw; }
        return ByteValue;
    }




public void ShowDataList()
    {
        SqlConnection conn = new SqlConnection(@"server=DEL-075\SQLEXPRESS;trusted_connection = yes;database = sample;");

        string sql1 = "select * from emp where emp_image is not null";

        SqlDataAdapter da = new SqlDataAdapter(sql1, conn);
        DataSet ds = new DataSet();

        da.Fill(ds, "DataL");

        DataList1.DataSource = ds.Tables["DataL"];
        DataList1.DataBind();

    }


Step 2: Make Page Named Picture.aspx and Write this Code on its   Load Event.



protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"server=ADIMPS-DEL-075\SQLEXPRESS;trusted_connection = yes;database = sample;");

        string sql1 = "Select Emp_Image from emp where id='" +Request.QueryString["id"] + "'";

        SqlCommand cmd1 = new SqlCommand(sql1, conn);
        conn.Open();

        SqlDataReader dr = cmd1.ExecuteReader();

        if (dr.Read())
        {
            if (dr["Emp_Image"].ToString() != System.DBNull.Value.ToString())
            {

                byte[] imageByte = (byte[])dr["Emp_Image"];

                if (imageByte != null && imageByte.Length > 0)
                {
                    Context.Response.ContentType = "image/jpeg";
                    Context.Response.BinaryWrite(imageByte);
                }
            }
        }

        dr.Close();
        conn.Close();
    }




How to Pass from Code Behind:
ProdImg.ImageUrl = "Picture.aspx?id=" + Val(TxtProdId.Text)


How to Pass for bind in Gridview,Datalist,ListView,Datagrid etc. as Given Below: 

-->
<asp:Image ID="Image2" runat="server" Height="80px" ImageUrl='<%# "Picture.aspx?id=" + Eval("Product_id")%>' Width="80px" />