Tuesday, 28 May 2013

Implicit conversion from data type sql_variant to uniqueidentifier is not allowed. Use the CONVERT function to run this query


Scenario – I was implementing the basic CRUD operations (Create, Read, Update and Delete) by using SQLDataSource into a gridview. I defined the DataKeyNames =’Id’ inside the gridview for the primary key reference(Primary key - Id is having the datatype as UniqueIdentifier into the database). Create, Read and Delete operations has been implemented successfully, but when I tried for the update operation it was showing the below error  – 



I checked my code thoroughly and understood the problem, please have a look below-

Problem found After matching the datatype for the parameters between frontend and backend, problem has been understood. Please go through the below points -


Point 1 - Please have a look at the below stored procedure which is used in this scenario for update the records
Point 2 - Please have a look at the aspx code from where I am passing the parameters to the StoredProcedure

So the main problem is -
When we pass Id from the forntend (which type is object)to the backend (StoredProcedure ,Where Id is type of UniqueIdentifier) then both are not matching.Solution – I removed the type from the parameter of Id. 



Please try,This solved my problem. 










Monday, 20 May 2013

Sending an email by using C# Asp.Net


In this article I will explain how to send email using asp.net.

Sending e-mails with ASP.NET is very simple and straight forward. Microsoft .NET framework comes with an entire namespace for handling e-mails, - System.Net.Mail namespace. In the following examples, we will use two classes from this namespace: MailMessage class for the actual e-mail, and the SmtpClient class, for SMTP server.

C# .Net is very powerfull programming language. To send an email we require a SMTP server.
There are few companies who provides SMTP server for free of cost like gmail. Here we will use SMTP of the gmail as it is free of cost.

High level understanding -
for mailing through SMTP server of the gmail, we require :  
1. SMTP server name and port number(smtp.gmail.com and 587 for gmail). 
2. A valid gmail account of the sender along with username and password.
3. Recipient email address. 

Implementation -
Please follow below steps to send an email by using C# Asp.Net.

Step 1 : Create a class to implement a function to send an email.

namespace: using System.Net.Mail;

    /// <summary>
    /// Function for send an email
    /// </summary>
    /// <param name="from"></param>
    /// <param name="to"></param>
    /// <param name="cc"></param>
    /// <param name="bcc"></param>
    /// <param name="subject"></param>
    /// <param name="body"></param>
    /// <param name="IsHTML"></param>
    public static void SendMailMessage(string from, string to, string cc, string bcc, string subject, string body,bool IsHTML)
    {
        //Create an instance of mail message
        MailMessage tMailMessage = new MailMessage();
        //Assign sender email address
        tMailMessage.From = new MailAddress(from);
        //Assign recipient email address
        tMailMessage.To.Add(new MailAddress(to));
        //Check if cc is not null
        if(cc!=null && cc!="")
            tMailMessage.CC.Add(new MailAddress(cc));
        //Check if bcc is not null
        if (bcc != null && bcc != "")
            tMailMessage.Bcc.Add(new MailAddress(bcc));
        //Assign the subject 
        tMailMessage.Subject = subject;
        //Assign the mail body
        tMailMessage.Body = body;
        //assign the format of the mail body
        tMailMessage.IsBodyHtml = IsHTML;
        // Assign the priority of the mail message to normal
        tMailMessage.Priority = MailPriority.Normal;
        //Subject encoding by UTF-8
        tMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
        //Body encoding by UTF-8
        tMailMessage.BodyEncoding = System.Text.Encoding.UTF8;

        //Create a new instance of SMTP client and pass name and port number         
       //of the smtp gmail server 
        SmtpClient tSmtpClient = new SmtpClient("smtp.gmail.com",587);
        //Enable SSL of the SMTP client
        tSmtpClient.EnableSsl = true;
        //Use delivery method as network
        tSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        //Use DefaultCredentials set to false
        tSmtpClient.UseDefaultCredentials = false;
        //Pass account information of the sender
        tSmtpClient.Credentials = new System.Net.NetworkCredential("yourmail@gmail.com", "*********");
        tSmtpClient.Send(tMailMessage);
    }

Step 2 : Create an user interface to take email parameters.
<table>
    <tr>
    <td colspan="2">Send mail by using ASP.NET</td>
    </tr>
    <tr>
    <td>
    To
    </td>
    <td>
    <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    CC
    </td>
    <td>
    <asp:TextBox ID="txtCC" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    BCC
    </td>
    <td>
    <asp:TextBox ID="txtBCC" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    Subject
    </td>
    <td>
    <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
    </td>
    </tr>
        <tr>
    <td>
    Body
    </td>
    <td>
    <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <asp:Button ID="btnSend" runat="server" Text="Send" onclick="btnSend_Click" />
    </td>
    </tr>
    </table>















Step 3 : Write code the event handler of the send button.
  • Create a region to declare global variable :
 #region Declaration
    private string _from = "";
    private string _to = "";
    private string _cc = "";
    private string _bcc = "";
    private string _subject = "";
    private string _body = "";
 #endregion


  • write code for the click event of send button
    /// <summary>
    /// Event handler: click event of the send button
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSend_Click(object sender, EventArgs e)
    {
        //assign values to the global variables from user interface/manually
        _from="vipulsachan.kiet@gmail.com";
        _to=txtTo.Text.Trim();
        _cc=txtCC.Text.Trim();
        _bcc=txtBCC.Text.Trim();
        _subject=txtSubject.Text.Trim();
        _body=txtBody.Text.Trim();
     
        //send mail
        EmailService.SendMailMessage(_from, _to, _cc, _bcc, _subject, _body, true);
    }


For video click here






Saturday, 18 May 2013

Event handler using JQuery



Click event handler of a button using JQuery-
when mouse pointer is over the button and mouse button is pressed and released then click event handler of the button will be fired. We can implement some logic/rules into this handler based on the business requirements.

creating click event handler on a button could be required in two types of scenario-
Scenario 1: when button is existing into the form or direct on the page.
Scenario 2: when button is existing inside another control like grid-view, repeater or any other control.

We will check with both the scenario one by one, Let's see the first scenario -

Scenario 1

when button is existing into the form or direct on the page
Let's follow the below steps :
Step 1 -Add a button to your aspx page under body section with an unique id
<body>
    <form id="form1" runat="server">
    <button type="button" id="formButtonClick">Click!</button>
    </form>
</body>


Step 2 - add the jquery-1.9.1.js file(click here to download) into head section of the aspx page.  
<head runat="server">
    <title></title>
   <script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>
</head>


Step 3 - Define the event handler and required function
<head runat="server">
    <title></title>
<script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>


<script type="text/javascript">
        $(document).ready(
        function () {
            //Call function to bind event handler
             BindEventHandler();
        });
        //Function definition for binding the event handler
        function BindEventHandler() {
            $("#formButtonClick").on("click", clickeventonformButton1);
        }
        //explain the event handler
        function clickeventonformButton1() {
            alert($(this).text());
        }
    </script>
</head>


Step 4- Right click on the page and 'View in Browser'



Click on the button -  as per our logic alert popup will come










Scenario 2 


when button is existing inside another control like grid-view  repeater or any other control
Let's follow the below steps:
Step 1 - Add a gridview to your aspx page under body section with an unique id and create a button inside it in a column


<body>
    <form id="form1" runat="server">
   
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField HeaderText="Action">
    <ItemTemplate>
    <button type="button" class="gridbutton"><%# Eval("Serial") %></button>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    </form>
</body>

Step 2 - Same as above


Step 3 - Define the event handler and required function

<head runat="server">
    <title></title>
   <script src="Scripts/jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
        $(document).ready(
        function () {
            //Call function to bind event handler
             BindEventHandler();
                  });
        //Function definition for binding the event handler
        function BindEventHandler() {
             $("#Gridview1").on("click",".gridbutton", clickeventonformButton1);
        }
        //explain the event handler
        function clickeventonformButton1() {
            alert($(this).text());
        }
    </script>
</head>


Step 4 - Right click on the page and View in Browser


Click on the button -  as per our logic alert popup will come