Thursday 28 August 2014

Row number for grouped data in RDLC Report



Rownumber function returns the number of the row of the dataset

Student_Name
Raja
Durai
Raja Durai
Raja Durai
Raja Durai
S.R Durai

If we would like to groupby Student _Name and show the row number beside each row, the result will be :

Row number              Student _Name
1                                 Raja
2                                 Durai
3                                 Raja Durai
6                                 S.R Durai

As you can see the row number value is not correct, actually it is correct because the rownumber function returns the number of row inside the dataset not inside the grouped data in the report

I am going to use runningvalue function for the grouped data inside "table1"

=runningvalue(Fields!Student _Name.Value,countdistinct,"table1")

Result:

Rownumber              Student _Name
1                                 Raja
2                                 Durai
3                                 Raja Durai
4                                 S.R Durai

Thursday 21 August 2014

Ajax ColorPicker in ASP.NET



       Now I’m going to explain how to use Ajax ColorPicker in ASP.NET. Ajax ColorPickerExtender provides client-side color-picking functionality with UI in a popup control.First you will add ajaxtoolkitcontrol.dll reference in your project. And give the below code in your aspx page.

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="actk" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script type="text/javascript">
        function Color_Changed(sender) {
            sender.get_element().value = "#" + sender.get_selectedColor();
        }
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:TextBox ID="txtColor" runat="server" Width="150px" />
        <br />
        <div id="PreviewColor" style="width: 50px; height: 50px; border: 1px solid Gray">
        </div>
        <asp:Button ID="btnPicker" runat="server" Text="Choose Color" />
        <actk:ColorPickerExtender ID="ColorPicker1" runat="server" TargetControlID="txtColor"
            SampleControlID="PreviewColor" PopupButtonID="btnPicker" PopupPosition="Right"
            OnClientColorSelectionChanged="Color_Changed" />
    </div>
</asp:Content>

 Then run your project, The result will display same as given below.



 

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