Tuesday, April 24, 2012

Equivalent Split function in SqlServer

Several times we have a collection of unique ids and we have to do some operation like update or delete based upon those ids.Now Given below function works as split and send all  values to the table.
This function basically needs three parametes-:
1) a string containg values seperated by some delimeter
2) delimiter
3)TrimSpace option( bit type to kill whitespaces)

Create FUNCTION [dbo].[fn_String_To_Table] (
            @String VARCHAR(max), /* input string */
   @Delimeter char(1),   /* delimiter */
   @TrimSpace bit )      /* kill whitespace? */
RETURNS @Table TABLE ( [Val] VARCHAR(4000) )
    DECLARE @Val    VARCHAR(4000)
    WHILE LEN(@String) > 0
        SET @Val    = LEFT(@String,
             ISNULL(NULLIF(CHARINDEX(@Delimeter, @String) - 1, -1),
        SET @String = SUBSTRING(@String,
             ISNULL(NULLIF(CHARINDEX(@Delimeter, @String), 0),
             LEN(@String)) + 1, LEN(@String))
  IF @TrimSpace = 1 Set @Val = LTRIM(RTRIM(@Val))
    INSERT INTO @Table ( [Val] )
        VALUES ( @Val )

Now here is a Stored Procedure using above function to update table.

create PROCEDURE [dbo].[sp_AdminDelStudEntry]
@RegNoString varchar(max)
UPDATE [dbo].[Admission] SET [Isdelete] = 'Y'
    WHERE [Reg_Noid] in (SELECT Val FROM [dbo].[fn_String_To_Table](@RegNoString,',',1))
UPDATE [dbo].[Academic] SET [Isdeleted] = 'Y'
    WHERE [regno] in (SELECT Val FROM [dbo].[fn_String_To_Table](@RegNoString,',',1))

CSS for Rounded Corner TextBox

A simple example for implementing rounded corner on TextBox using CSS.
 <style type="text/css">

.roundedbox {
    font-family:Verdana,Arial, Helvetica, sans-serif;
    border-top:1px solid #CCCCCC;
    border-left:1px solid #CCCCCC;
    border-right:1px solid #999999;
    border-bottom:1px solid #999999;
    -moz-border-radius: 8px;
    -webkit-border-radius: 8px;


<asp:TextBox ID="TextBox1" runat="server" CssClass="roundedbox" Width="300px">

And here the output :

Saturday, April 21, 2012

Left or Right alignment in Gridview Columns

When we work with Grid-view we need to make left or right or centered alignment on column values according to their Datatype.For example we have to make right aligned all money values and character values to be left aligned. To do this just embed column within table like given example -:

 <asp:TemplateField HeaderText="Debit">
                    <table align="right"  ><tr><td>
                                <asp:Label ID="lblDebit" runat="server" Text='<%# Bind("Debit") %>'></asp:Label>

Hope you'll get it. Enjoy coding.

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster

Here are few steps to fix "Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster" error.

Step 1 : Visit this link and generate your machine key.

Step 2 : Copy the machine key given by above link and paste it to your web.config file.

<?xml version=”1.0″?>



       <machineKey validationKey='4C8B22744D71B73CE6B16262A97CF3862E584829229EDE45C9DF207DDF5F3507782A3F2F6506085028628868F6AFFC25D16BE4AD8BB677637B11C892F95FE819'   decryptionKey='53EA49CFEC5C5A1E4720DE83E1CC75337674757144EB1DCB'   validation='SHA1'/>


Step 3 : By doing above two steps you will be relieved by this error.But you must know why this error occurs.To get a good detail about this error you must visit this link.


Friday, April 20, 2012

Send GridView in Mail in asp.net,C#

This example makes u enable to understand how to email server controls information like Gridview as it is.
Now I am considering that we have a web form which contains a GridView (containing Data) and a button
which will be used to send email.

At first include these namespaces to your code behind.

using System.Net.Mail;
using System.Text;
using System.IO;

Now in Button Click event write this code :

protected void ibMail_Click(object sender, ImageClickEventArgs e)
        string to = "anukana@symtechindia.net";
        string From = "mafire5@gmail.com";
        string subject = "Balance Detail";
        string Body = "Dear sir ,<br> Plz Check d Attachment <br><br>";
        Body += GridViewToHtml(gvPArtyStatement); //Elaborate this function detail later
        Body += "<br><br>Regards,<br>Anukana";
        bool send = send_mail(to, From, subject, Body);//Elaborate this function detail later
        if (send == true)
            string CloseWindow = "alert('Mail Sent Successfully!');";
            ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);
            string CloseWindow = "alert('Problem in Sending mail...try later!');";
            ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", CloseWindow, true);

send_mail() Definition :

public bool send_mail(string to, string from, string subject, string body)
            MailMessage msg = new MailMessage(from, to);
            msg.Subject = subject;
            AlternateView view;
            SmtpClient client;
            StringBuilder msgText = new StringBuilder();
            msgText.Append(" <html><body><br></body></html> <br><br><br>  " + body);
            view = AlternateView.CreateAlternateViewFromString(msgText.ToString(), null, "text/html");

            client = new SmtpClient();
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.Credentials = new System.Net.NetworkCredential("jhalpharegi@gmail.com", "Your_password");
            client.EnableSsl = true; //Gmail works on Server Secured Layer
            bool k = true;
            return k;

GridViewToHtml() definition :

 private string GridViewToHtml(GridView gv)
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        return sb.ToString();
    public override void VerifyRenderingInServerForm(Control control)
         //Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.

Now browse and send mail and output will be like this one -:

Sometime one can  encountered by this error  -:
RegisterForEventValidation can only be called during Render();

This means that either you have forgot to override VerifyRenderingInServerForm in code behind or EventValidation is true.
So the solution is set EventValidation to false and must override VerifyRenderingInServerForm method.

Tuesday, April 10, 2012

Open outlook in Asp.net(C#) only in 3 steps

Open outlook in Asp.net(C#) only in 3 steps-:
Step 1 :  At first  add the namespace for outlook  and if its not showing in intellisense than add reference for Microsoft.Outlook
using Microsoft.Office.Interop.Outlook;
Step 2 : Bind Data in Grid Which contains Mail Address.Keep Mail Id in Template field in this format

<asp:TemplateField HeaderText="Email" SortExpression="Email">
<asp:LinkButton ID="lbSendMail" runat="server"  CommandName="SendMail"
CommandArgument='<%# Bind("Email") %>' Text='<%# Bind("Email") %>' ></asp:LinkButton>

Step 3 : Fire the RowCommand Event of GridView and Write down the Code Given Below.

if (e.CommandName == "SendMail")
   Microsoft.Office.Interop.Outlook.Application oApp = new                Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem oMailItem = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(OlItemType.olMailItem);
  oMailItem.To = e.CommandArgument.ToString();          
  //Similary U can Add another Detail like body, bcc etc...

Now Run Your Programme and output will be like this :
After Clicking on Any mail Id a new window popup like this one :

Note : To get proper output you have to configure Outlook in your System.

And Finally Here is the Source Code :

Wednesday, April 4, 2012

Value does not fall within the expected range in rdlc

Whenever we encountered by this error in rdlc, it simply means that we are assigning whole dataset to ReportDatasource.

 ReportDataSource rds = new ReportDataSource("MinQtyItem_sp_MinQtyItem", ds);

To overcome this issue just tell ReportDataSource which table of DataSet She/He has to load.

 ReportDataSource rds = new ReportDataSource("MinQtyItem_sp_MinQtyItem", ds.Tables[0]);