March 16, 2012 Leave a comment

Originally posted on Raymund Macaalay's Dev Blog:

I just want to share how I make my paging and sorting in C#.  I had made a lot of styles before but this is the one that I used most, so in all of my all of my Grid Views here is what I do.

View original 819 more words

Categories: Uncategorized

Creating HotSpots for Image and using it with ASP.Net ImageMap Control

*Download*

1) First download the MapEdit trial software and install it.
MapEdit Download

2) Open a image which we want to use in ImageMap control. {File-New Html Document(Open Image) Ctrl+N}

3)After opening image we can create Rectangle, Circle, and Polygon hotspots with that MapEdit tool.

A) Creating Rectangle spot on image and  getting coordinates of rectangle and using these coordinates in the ImageMap control of asp.net
For creating rectangle spot on image, just go to ‘Tools’ option and select ‘Rectangle’ and draw rectangle
shape on image

After drawing rectangle shape to get the coordinates just click on ‘View’ and select ‘Show Html Tags’.
When we click on ‘Show Html Tags’ , it will open one txt file , from this file we can get the coords like
‘  area shape=”rect” alt=”” coords=”227,78,361,110″ nohref title=””  ‘
Here coords are represented as the coords(x1,y1,x2,y2) / coords(left,top,right,bottom).
These coords can be consumed in HotSpots Property of ImageMap control.

B) Creating Circle spot on image and  getting coordinates of circle and using these coordinates in the ImageMap control of asp.net
For creating circle spot on image, just go to ‘Tools’ option and select ‘Circle’ and draw rectangle
shape on image

After drawing circle shape to get the coordinates just click on ‘View’ and select ‘Show Html Tags’.
When we click on ‘Show Html Tags’ , it will open one txt file , from this file we can get the coords like
‘  area shape=”circle” alt=”Circle” coords=”305,187,68″ nohref title=”Circle”   ‘

Here coords are represented as the coords(x,y,r) / coords(X,Y,Radius).
These coords can be consumed in HotSpots Property of ImageMap control.

C) Creating Polygon spot on image and  getting coordinates of polygon and using these coordinates in the ImageMap control of asp.net
For creating polygon spot on image, just go to ‘Tools’ option and select ‘Circle’ and draw rectangle
shape on image

‘  area shape=”poly” alt=”Polygon” coords=”126,79,181,79,155,115,180,157,180,257,159,301,138,261,123,222,150,217,
122,196,122,143,127,122,119,49,177,43,177,58,180,70″ nohref title=”Polygon”   ‘

Here coords are represented as the coords(x1,y1,x2,y2,x3,y3…..) .
These coords can be consumed in HotSpots Property of ImageMap control.

Categories: ASP.Net

DataAccessLayer.cs for MS SQL Server in .Net

   1:   
   2:  using System;
   3:  using System.Data;
   4:  using System.Configuration;
   5:  using System.Data.SqlClient;
   6:   
   7:  ///
 8: /// DataAccessLayer class is sealed class(which cannot be inherited). 
 9: /// It contains two public static methods. 
 10: /// First one is ExecuteDMLCommand and another one is ExecuteDQLCommand. 
 11: /// 
  12:  public sealed class DataAccessLayer
  13:  {
  14:   
  15:      #region Fields
  16:   
  17:      static SqlConnection con;
  18:      static SqlCommand cmd;
  19:      static DataSet ds;
  20:      static SqlDataAdapter da;
  21:      //static SqlTransaction trans;
  22:   
  23:      #endregion
  24:   
  25:      #region StaticConstructor
  26:   
  27:      ///
 28: /// Constructor. 29: /// 

  30:      static DataAccessLayer()
  31:      {
  32:          con = null;
  33:          cmd = null;
  34:          ds = null;
  35:          da = null;
  36:          //trans = null;
  37:      }
  38:   
  39:      #endregion
  40:   
  41:      #region StaticPrivateMethods
  42:   
  43:      ///
 44: /// Returns SQL Sever connection string. 
 45: /// 
  46:      /// Connection String.
  47:      static string GetSqlConnectionString()
  48:      {
  49:          return
  50:              ConfigurationManager.ConnectionStrings["SqlConnectionString"].
  51:              ConnectionString.ToString();
  52:      }
  53:   
  54:      ///
 55: /// Sets SqlCommand object. 
 56: /// 
  57:      ///cmd">SqlCommand field.
  58:      ///commandText">SQL command text.
  59:      ///SqlConnection object.
  60:      ///commandType">CommandText or StoredProcedure.
  61:      ///storedProcedureParameters">Parameters of stored procedure.
  62:      /// SqlCommand object.
  63:      static SqlCommand SetSqlCommand
  64:          (SqlCommand cmd, string commandText, SqlConnection con,
  65:          CommandType commandType, SqlParameter[] storedProcedureParameters)
  66:      {
  67:          try
  68:          {
  69:              cmd = new SqlCommand(commandText, con);
  70:              cmd.CommandType = commandType;
  71:              if (storedProcedureParameters != null &&
  72:                            storedProcedureParameters.Length != 0)
  73:              {
  74:                  foreach (SqlParameter param in storedProcedureParameters)
  75:                  {
  76:                      cmd.Parameters.Add(param);
  77:                  }
  78:              }
  79:              return cmd;
  80:          }
  81:          catch (Exception ex)
  82:          {
  83:              throw new ArgumentException(ex.Message);
  84:          }
  85:      }
  86:   
  87:      #endregion
  88:   
  89:      #region StaticPublicMethods
  90:   
  91:      ///
 92: /// Executes Data Manipulation Language commands(Insert, Update, Delete). 
 93: /// 
  94:      ///nonQueryCommandText">Insert/Update/Delete command statement.
  95:      ///commandType">CommandText or StoredProcedure.
  96:      ///storedProcedureParameters">Parameters of stored procedure.
  97:      /// Number of rows affected.
  98:      public static int ExecuteDMLCommand
  99:          (string nonQueryCommandText, CommandType commandType,
 100:          SqlParameter[] storedProcedureParameters)
 101:      {
 102:          try
 103:          {
 104:              con = new SqlConnection(GetSqlConnectionString());
 105:              cmd = SetSqlCommand(cmd, nonQueryCommandText, con,
 106:                             commandType, storedProcedureParameters);
 107:              con.Open();
 108:              //trans = con.BeginTransaction();
 109:              //cmd.Transaction = trans;
 110:              int numberOfRowsAffected = cmd.ExecuteNonQuery();
 111:              //trans.Commit();
 112:              return numberOfRowsAffected;
 113:          }
 114:          catch (SqlException ex)
 115:          {
 116:              //trans.Rollback();
 117:              throw new ArgumentException(ex.Message);
 118:          }
 119:          catch (Exception ex)
 120:          {
 121:              //trans.Rollback();
 122:              throw new ArgumentException(ex.Message);
 123:          }
 124:          finally
 125:          {
 126:              if (con != null)
 127:              {
 128:                  if (con.State == ConnectionState.Open)
 129:                  {
 130:                      con.Close();
 131:                  }
 132:              }
 133:          }
 134:      }
 135:   
 136:      ///
 137: /// Executes Data Query Language command(Select). 
 138: /// 
 139:      ///selectCommandText">Select command statement.
 140:      ///CommandText or StoredProcedure.
 141:      ///Parameters of stored procedure.
 142:      /// Data in the form of DataSet.
 143:      public static DataSet ExecuteDQLCommand
 144:          (string selectCommandText, CommandType commandType,
 145:          SqlParameter[] storedProcedureParameters)
 146:      {
 147:          try
 148:          {
 149:              con = new SqlConnection(GetSqlConnectionString());
 150:              cmd = SetSqlCommand(cmd, selectCommandText, con,
 151:                     commandType, storedProcedureParameters);
 152:              using (da = new SqlDataAdapter(cmd))
 153:              {
 154:                  ds = new DataSet();
 155:                  da.Fill(ds);
 156:              }
 157:              return ds;
 158:          }
 159:          catch (SqlException ex)
 160:          {
 161:              throw new ArgumentException(ex.Message);
 162:          }
 163:          catch (Exception ex)
 164:          {
 165:              throw new ArgumentException(ex.Message);
 166:          }
 167:      }
 168:   
 169:      #endregion
 170:   
 171:  }
 172:
Categories: C#.Net

Format C# Code in Blogspot post

Steps to format C# code.

1) Copy the C# code and paste it in the C# code formatter(LINK) TextBox.
2) Check the embed stylesheet checkbox.
3) Click on format my code button.
4) Copy the html code which is generated by that link and paste it in your blogspot post in Edit HTML view of  rich textbox.

After formatting C# code, it looks like below.

   1:  using System;
   2:  using System.Data;
   3:  using System.Configuration;
   4:  using System.Data.SqlClient;
   5:   
   6:  /// <summary>
   7:  /// Summary description for DataAccessLayer
   8:  /// </summary>
   9:   
  10:  public class DataAccessLayer
  11:  {
  12:      public static string GetConnectionString()
  13:      {
  14:          return ConfigurationManager.ConnectionStrings["ConnectionString"].
  15:              ConnectionString.ToString();
  16:      }
  17:   
  18:   
  19:      static SqlConnection con;
  20:   
  21:      public static int ExecuteNonQuery
  22:          (string connectionString, CommandType commandType, 
  23:          string commandText, SqlParameter[] parameters)
  24:      {
  25:          try
  26:          {
  27:              con = new SqlConnection(connectionString);
  28:              SqlCommand cmd = new SqlCommand(commandText, con);
  29:              cmd.CommandType = commandType;
  30:              foreach (SqlParameter p in parameters)
  31:              { cmd.Parameters.Add(p); }
  32:              con.Open();
  33:              return cmd.ExecuteNonQuery();
  34:          }
  35:          catch (SqlException ex)
  36:          { throw new ArgumentException(ex.Message); }
  37:          finally { con.Close(); }
  38:      }
  39:   
  40:   
  41:      public static DataSet ExecuteDataSet
  42:          (string connectionString, CommandType commandType, 
  43:          string commandText, SqlParameter[] parameters)
  44:      {
  45:          try
  46:          {
  47:              SqlConnection sqlcon = new SqlConnection(connectionString);
  48:              SqlCommand cmd = new SqlCommand(commandText, sqlcon);
  49:              cmd.CommandType = commandType;
  50:              foreach (SqlParameter p in parameters)
  51:              { cmd.Parameters.Add(p); }
  52:              DataSet ds;
  53:              using (SqlDataAdapter da = new SqlDataAdapter(cmd))
  54:              {
  55:                  ds = new DataSet();
  56:                  da.Fill(ds);
  57:              }
  58:              return ds;
  59:          }
  60:          catch (SqlException ex)
  61:          { throw new ArgumentException(ex.Message); }
  62:      }
  63:   
  64:  }
Link to format C# code is http://www.manoli.net/csharpformat/
Categories: Miscellaneous

GridView inside another GridView in ASP.NET

 

In RowDataBound event of GridView, we find the inner GridView control after every row data bound and we bind the data to that inner GridView in order to achieve NestedGridView.
Design the GridView as per below.

Set the “AutoGenerateColumns” property of GridView to false. And place the chlid GridView in ItemTemplate of ‘asp:TemplateField’ tag of parent GridView. Write the code in RowDataBound event to find the inner GridView and assign data source to that inner control.

Steps to create NestedGridView in ASP.Net.

Step:1(Database Layer)

Create Database with the name “NestedGridView”  as following
Create Database NestedGridView

In that NestedGridView database create three(3) tables  as shown in below image.

 

Write the stored procedures(3) like below

i)
Create Procedure sp_GetFileDetails ( @FileId int )
As
Select * from tbl_Files where FileId = @FileId
ii)
Create procedure sp_GetFilesFromCart
As
Begin
Select * from tbl_Cart c inner join tbl_Files f on f.FileId = c.FileId
End
iii)

Create procedure sp_GetPrintSize
As
Select * from tbl_PrintSize



Step:2(DataAccessLayer And BusinessLogicLayer)

i) Write the DataAccessLayer like this.

ii) Business Logic Layer Class:

   1:  using System;
   2:  using System.Data;
   3:  using System.Data.SqlClient;
   4:   
   5:  public class BusinessLogicLayer
   6:  {
   7:      public static DataSet GetFilesFromCart()
   8:      {
   9:          try
  10:          {
  11:              SqlParameter[] p = new SqlParameter[0];
  12:              return DataAccessLayer.ExecuteDataSet
  13:                  (
  14:                  DataAccessLayer.GetConnectionString(), 
  15:                  CommandType.StoredProcedure, "[sp_GetFilesFromCart]", p
  16:                  );
  17:          }
  18:          catch (Exception ex) { throw new ArgumentException(ex.Message); }
  19:      }
  20:   
  21:      public static DataSet GetPrintSize()
  22:      {
  23:          try
  24:          {
  25:              SqlParameter[] p = new SqlParameter[0];
  26:              return DataAccessLayer.ExecuteDataSet
  27:                  (
  28:                  DataAccessLayer.GetConnectionString(), 
  29:                  CommandType.StoredProcedure, "sp_GetPrintSize", p
  30:                  );
  31:          }
  32:          catch (Exception ex)
  33:          {
  34:              throw new ArgumentException(ex.Message);
  35:          }
  36:      }
  37:   
  38:      public static DataSet GetFileDetails(int fileId)
  39:      {
  40:          try
  41:          {
  42:              SqlParameter[] p = new SqlParameter[1];
  43:              p[0] = new SqlParameter("@FileId", fileId);
  44:              return DataAccessLayer.ExecuteDataSet
  45:                  (
  46:                  DataAccessLayer.GetConnectionString(), 
  47:                  CommandType.StoredProcedure, "sp_GetFileDetails", p
  48:                  );
  49:          }
  50:          catch (Exception ex)
  51:          {
  52:              throw new ArgumentException(ex.Message);
  53:          }
  54:      }
  55:  }

Step:3(PresentationLayer)

Default.aspx Page Source Code:

   1:  GridView ID="GridView1" runat="server" 
   2:  AutoGenerateColumns="False" 
   3:  ShowHeader="true" Width="500px" 
   4:  onrowdatabound="GridView1_RowDataBound">
   5:  <Columns>
   6:   
   7:  TemplateField  ItemStyle-BorderStyle="None" 
   8:                   HeaderText="S No"  ItemStyle-Font-Bold="true">
   9:  <ItemTemplate>
  10:  <%# Container.DataItemIndex + 1  %>
  11:  </ItemTemplate>
  12:  TemplateField>
  13:   
  14:  TemplateField HeaderText="Selected Photos">
  15:  <ItemTemplate>
  16:  <table>
  17:  <tr>
  18:  <td valign="middle">
  19:  ImageButton ID="ImageButton1" Enabled="false"
  20:    CommandName="selectedimg" 
  21:    ImageUrl='<%# "~/Thumbnail.ashx?fileId="+Eval("FileId") %>'
  22:     CommandArgument='<%# Eval("FileId") %>' runat="server">
  23:  ImageButton>
  24:  </td>
  25:  </tr>
  26:  </table>
  27:  </ItemTemplate>
  28:  TemplateField>
  29:   
  30:  TemplateField HeaderText="Print Options">
  31:  <ItemTemplate>
  32:  <%--Start ChildGridView--%>
  33:  GridView ID="GridView2" runat="server" 
  34:  ShowHeader="true" Width="300px" AutoGenerateColumns="false">
  35:  <Columns>
  36:  TemplateField HeaderText="Qty">
  37:  <ItemTemplate>
  38:  TextBox ID="TextBox1" MaxLength="3" ValidationGroup="add"
  39:    Width="35" onkeydown="return CheckKeyCode()" runat="server">
  40:  </asp:TextBox>
  41:  </ItemTemplate>
  42:  TemplateField>
  43:   
  44:  <asp:TemplateField HeaderText="Size">
  45:  <ItemTemplate>
  46:  LinkButton ID="LinkButton1" ForeColor="AliceBlue" 
  47:  CommandName="lbtnsize" Enabled="false" Text='<%# Eval("SizeName") %>'
  48:  CommandArgument='<%# Eval("SizeId") %>' runat="server">
  49:  LinkButton>
  50:  </ItemTemplate>
  51:  </asp:TemplateField>
  52:  Qty)">
  53:  <ItemTemplate>
  54:  <asp:LinkButton ID="LinkButton2" ForeColor="AliceBlue"
  55:   Text='<%# Eval("Price") %>' Enabled="false" CommandName="lbtnprice"
  56:  CommandArgument='<%# Eval("SizeId") %>' runat="server">
  57:  </asp:LinkButton>
  58:  </ItemTemplate>
  59:  </asp:TemplateField>
  60:   
  61:  <asp:TemplateField HeaderText="TotalPrice">
  62:  <ItemTemplate>
  63:  <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
  64:  </ItemTemplate>
  65:  </asp:TemplateField>
  66:   
  67:  </Columns>
  68:  GridView>
  69:  <%--End ChildGridView--%>
  70:  </ItemTemplate>
  71:  </asp:TemplateField>
  72:   
  73:  </Columns>
  74:  GridView>



Default.aspx.cs Code:

   1:  using System;
   2:  using System.Data;
   3:  using System.Web.UI.WebControls;
   4:   
   5:  public partial class _Default : System.Web.UI.Page 
   6:  {
   7:      protected void Page_Load(object sender, EventArgs e)
   8:      {
   9:          if (!IsPostBack)
  10:          {
  11:              DataSet ds = BusinessLogicLayer.GetFilesFromCart();
  12:              GridView1.DataSource = ds;
  13:              GridView1.DataBind();
  14:          }
  15:      }
  16:   
  17:      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  18:      {
  19:          if (e.Row.RowType == DataControlRowType.DataRow)
  20:          {
  21:              GridView gv = (GridView)e.Row.FindControl("GridView2");
  22:              DataSet ds1 = BusinessLogicLayer.GetPrintSize();
  23:              ImageButton ib = (ImageButton)e.Row.FindControl("ImageButton1");
  24:              gv.DataSource = ds1;
  25:              gv.DataBind();
  26:          }
  27:      }
  28:  }

Thumbnail.ashx:
<%@ WebHandler Language=”C#” Class=”Thumbnail” %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
public class Thumbnail : IHttpHandler {
public void ProcessRequest (HttpContext context) {
if (context.Request.QueryString["fileId"].ToString() == “”)
{ }
else
{
int fileId = Convert.ToInt32(context.Request.QueryString["fileId"]);
if (fileId != 0)
{
DataSet ds = BOL.GetFileDetails(fileId);
if (ds.Tables[0].Rows[0]["FileContent"] != System.DBNull.Value)
{
byte[] image = (byte[])ds.Tables[0].Rows[0]["FileContent"];
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ms.Write(image, 0, image.Length);
context.Response.Buffer = true;
if (image.Length != 0)
{
Bitmap loBMP = new Bitmap(ms);
int wid = loBMP.Width;
if (wid > 120) { wid = 120; }
int hei = loBMP.Height;
if (hei > 100) { hei = 100; }
Bitmap bmpOut = new Bitmap(wid, hei);
Graphics g = Graphics.FromImage(bmpOut);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, wid, hei);
g.DrawImage(loBMP, 0, 0, wid, hei);
MemoryStream ms2 = new MemoryStream();
bmpOut.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bmpBytes = ms2.GetBuffer();
bmpOut.Dispose();
ms2.Close();
context.Response.Clear();
context.Response.BinaryWrite(bmpBytes);
context.Response.End();
}
ms.Dispose();
}
}
}
}
public bool IsReusable {
get {
return false;
}
}
}

Categories: ASP.Net

Getting Month Name in Sql Server

In Sql Server DatePart function with part as a MM returns the month value. And DateName function with part as a MM returns the month name.

Select DATENAME(MM,Date) As MonthName  From Tbl_CompanySharesPrice

Categories: SQL Server

PascalCase & camelCase

Pascal case

The first letter of every word in the identifier must be capital.
For example: PascalCase , MethodName

Camel case

The first letter of first word in the identifier must be lowercase and the first letter of subsequent words in the identifier is capital.
For example: camelCase , arguementName

Categories: C#.Net
Follow

Get every new post delivered to your Inbox.