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);
    }

Thursday, October 28, 2010

Create Thumbnail in asp.net

System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload.InputStream);
 System.Drawing.Image thumbnailImage = img.GetThumbnailImage(25, 30, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
   img.save(stringPath);

select all checkbox in asp.net

 function SelectAll(id)
{
        var frm = document.forms[0];
        for (i=0;i
    {
            if (frm.elements[i].type == "checkbox")
        {
                frm.elements[i].checked = document.getElementById(id).checked;
                }
        }
}  

Place this text in your source File :
{         
       
   //  HeaderTemplate in tag <>  //

 // asp:CheckBox ID="chkAll" runat="server"  in tag <>//

 //  HeaderTemplate in tag <>//

      


}   
In Code File
  protected void grdmember_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //adding an attribute for onclick event on the check box in the header
            //and passing the ClientID of the Select All checkbox
            ((CheckBox)e.Row.FindControl("chkAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("chkAll")).ClientID + "')");
        }
    }