Tuesday, August 6, 2013

MAIL send from ASP.net C#

Here is a simple example for MAIL sending from ASP.net and C#

Hope it will help you



private void MAILSEND(string strDecision)
    {
        SmtpClient smtp = new SmtpClient();
        MailMessage mail = new MailMessage();

        mail.From = new MailAddress(Originator_mail address);
        mail.To.Add(mail_address);

mail.Subject ="Subject";
       mail.Body="Body";
smtp.Host ="Host_Addr";
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Port = 25;
            smtp.Send(mail)
}

client side popup message in asp.net

client side popup alert message for asp.net

1. please select the button properties
2. go the the "OnClientClick" properties
3.paste this syntax
 return confirm('Are you sure......?');


how to use session variables in ASP.Net

session variables is a important and necessary variable in asp.net, specially in web programming.
programmer can use session variable to keep the information for the particular session. i.e if you fill a session variable with data you can use this variable of dada any point of data for the particular session.

below is the example of session variable

Session["LoginUserID"]=txtuserid.text;

Web Service in C# with XML return format

I am writing here a web service in C# for xml output. please check the below example. this will surely help you.

[WebMethod(Description = "User Authentication")]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public List LoginAuthentication(string struserID, string strPassword)
    {
        SqlConnection objConnection = new SqlConnection(strconnectionString);
        SqlCommand objCommand = new SqlCommand("SELECT * FROM UserInformation where UserID='" + struserID + "' and Password='" + strPassword + "' ", objConnection);
        DataSet objDataSet = new DataSet();
        SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
        objDataAdapter.Fill(objDataSet, "reading");
        objConnection.Close();
        List colLatestUpdate = new List();

        foreach (DataRow rs in objDataSet.Tables[0].Rows)
        {
            LoginAuth objLU = new LoginAuth();
            objLU.Status = 1;
            colLatestUpdate.Add(objLU);          
        }

        if (objDataSet.Tables[0].Rows.Count == 0)
        {
            LoginAuth objLU = new LoginAuth();
            objLU.Status = 0;
            colLatestUpdate.Add(objLU);
        }
        XmlSerializer LatestUpdateserialize = new XmlSerializer(typeof(List));

        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        LatestUpdateserialize.Serialize(ms, colLatestUpdate);
        ms.Position = 0;
        List LatestUpdateElement = (List)LatestUpdateserialize.Deserialize(ms);
        return LatestUpdateElement;
    }



///Entity Class

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///
/// Summary description for LatestUpdate
///

public class LoginAuth
{
    public LoginAuth()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    private int _Status = 0;
    private string _userid;
    private string _mobile;
   
    public int Status
    {
        get
        {
            return _Status;
        }
        set
        {
            _Status = value;
        }
    }
    public string UserID
    {
        get
        {
            return _userid;
        }
        set
        {
            _userid = value;
        }
    }
    public string Mobile
    {
        get
        {
            return _mobile;
        }
        set
        {
            _mobile = value;
        }
    } 
}



Web Service in C# with JSON return format

You can very easily write web service with JSON data return format.

please check below method, this may help you.

[WebMethod(Description = "Gets Student information")]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetStudent()
    {
        SqlConnection objConnection = new SqlConnection(strconnectionString);
        SqlCommand objCommand = new SqlCommand("SELECT * FROM Student", objConnection);
        DataSet objDataSet = new DataSet();
        SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
        objDataAdapter.Fill(objDataSet, "reading");
        objConnection.Close();
        // Create a multidimensional jagged array
        string[][] JaggedArray = new string[objDataSet.Tables[0].Rows.Count][];
        int i = 0;
        foreach (DataRow rs in objDataSet.Tables[0].Rows)
        {
            JaggedArray[i] = new string[] { rs["Name"].ToString(), rs["Roll"].ToString(), rs["Class"].ToString() };
            i = i + 1;
        }
        // Return JSON data

        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(JaggedArray);
        return strJSON;

    }

how to do type casting in c#

frequently we need casting in c#. type casting is one of them as like below

private void findtotalstudent()
{
int count=(int)gettotalstudent()
}

private int gettotalstudent()
 {
int totalcount=0

// find out total count from  database

return totalcount;
}