Saturday, August 20, 2011

Sql Function To Split the commas Value and insert them into table.



CREATE FUNCTION [dbo].[ListToTable] (
  /*
  FUNCTION ListToTable
  Usage: select entry from listtotable('abc,def,ghi') order by entry desc
  PURPOSE: Takes a comma-delimited list as a parameter and returns the values of that list into a table variable.
  */
  @mylist varchar(8000)
  )
  RETURNS @ListTable TABLE (
  seqid int not null,
  entry varchar(255) not null)

  AS

  BEGIN
      DECLARE
              @this varchar(255),
              @rest varchar(8000),
              @pos int,
              @seqid int

      SET @this = ' '
      SET @seqid = 1
      SET @rest = @mylist
      SET @pos = PATINDEX('%,%', @rest)
      WHILE (@pos > 0)
      BEGIN
              set @this=substring(@rest,1,@pos-1)
              set @rest=substring(@rest,@pos+1,len(@rest)-@pos)
              INSERT INTO @ListTable (seqid,entry)  VALUES (@seqid,@this)
              SET @pos= PATINDEX('%,%', @rest)
              SET @seqid=@seqid+1
      END
      set @this=@rest
      INSERT INTO @ListTable (seqid,entry) VALUES (@seqid,@this)
      RETURN
  END

Some Important Example or Regular expression in asp.net



Remove last two zero inside gridview if
Price=56.0000:

'<%#Eval("Price").ToString().Remove(Eval("Price").ToString().Length - 2, 2) %>'


Regular Expressions for Amount:

^\$?\d+(\.(\d{2}))?$
To evaluate an amount with or without a dollar sign where the cents are optional.


^([0-9]*|\d*\.\d{1}?\d*)$
Accept only (0-9) integer and one decimal point(decimal point is also optional).After decimal point it accepts at least one numeric .This will be usefull in money related fields or decimal fields.

Regular Expression for multiline Textbox:

ErrorMessage="Address should be less than 130 characters" ValidationExpression="[\s\S]{0,130}"
ValidationGroup="submit">.


compare validate for today date:

Type="Date" runat="server" ValidationGroup="submit" ErrorMessage="Start date must be greater than today date."
SetFocusOnError="True">.



On page load

 c1.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy");


Friday, June 10, 2011

Asp.net MVC, Html.DropDownList Selected Value for EDIT

In Controller:

  ViewData["MyDropDown"] = new SelectList(CatModel.getAllCategory(), "Value", "Text",selectedValue:prModel.DisplayProductByid(prd).CategoryID);

In View:

 <%: Html.DropDownList("MyDropDownID", ViewData["MyDropDown"] as SelectList)%>

----------------------------------------------------------------------------
I have got same problem and the solution is above:
I just changed the Name of the drop down and handled the assignment in the controller.
The reason behind this problem is that asp.net MVC first looks for a match between the name of the drop down and a property on the model. If there’s a match, the selected value of the SelectList is overridden. Changing the name of the drop down is all it takes to remedy the issue.

Thursday, March 17, 2011

Send Multiple attachment in Mail

  public static void SendAttachmentMail(string MailBody, string EmailTo, string MailFrom, string Subject, string[] listpdf)
    {
        try
        {
            MailMessage sendMail = new MailMessage();
            SmtpClient smtp = new SmtpClient();
            NetworkCredential SmtpUser = new NetworkCredential();
            sendMail.To.Add(EmailTo);
            sendMail.From = new MailAddress(MailFrom);
            sendMail.Subject = Subject;
            sendMail.Body = MailBody;
            sendMail.IsBodyHtml = true;
          
            int cnt = listpdf.Count();
            for (int l = 0; l < cnt; l++)
            {
                Attachment at_l = new Attachment(listpdf[l]);
                sendMail.Attachments.Add(at_l);
            }
            smtp.EnableSsl = true;
            smtp.Send(sendMail);
            sendMail.To.Clear();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write(ex.Message.ToString());
            HttpContext.Current.Response.End();
        }
    }

Sunday, February 6, 2011

Create Barcode Image with asp.net

To create and save barcode image in database with asp.net :




private Bitmap createbarcode(string data)
    {
        Bitmap barcode = new Bitmap(1, 1);
        PrivateFontCollection pfc = new PrivateFontCollection();
        string pth = Server.MapPath("../Fonts/idautomationhc39m.ttf");
        pfc.AddFontFile(pth);
        FontFamily ff = pfc.Families[0];
        FontFamily family = new FontFamily("IDAutomationHC39M", pfc);
        System.Drawing.Font threeofnine = new System.Drawing.Font(family, 12, FontStyle.Regular, GraphicsUnit.Point);
        Graphics graphic = Graphics.FromImage(barcode);
        SizeF datasize = graphic.MeasureString(data, threeofnine);
        barcode = new Bitmap(barcode, datasize.ToSize());
        graphic = Graphics.FromImage(barcode);
        graphic.Clear(System.Drawing.Color.White);
        graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
        graphic.DrawString(data, threeofnine, new SolidBrush(System.Drawing.Color.Black), 0, 0);
        graphic.Flush();
        threeofnine.Dispose();
        graphic.Dispose();
        return barcode;
    }
    private void GenerateAndGetBarCode()
    {
        data = GenerateRandomCode();
        Bitmap barCode = createbarcode("*" + data + "*");
        imgPath = ConfigurationManager.AppSettings["site_url"] + "/Barcode/" + data + ".Gif";
        String filepath = Server.MapPath("../BarCode/") + data + ".Gif";
        barCode.Save( filepath, ImageFormat.Gif);
    }