Thursday, September 30, 2010

correct chatting

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class ChatRoom : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
    Class1 obj = new Class1();
    private void ddlbind()
    {
        obj.gettable("select * from Usertab where UserID!=" + Convert.ToInt32(Session["ChatUserID"]));

        DropDownList1.DataSource = obj.dt;
        DropDownList1.DataBind();
    }
    private string _callBackStatus;
   // this.InsertMessage(ConfigurationManager.AppSettings["ChatLoggedInText"] + " " + DateTime.Now.ToString());  
    private void GetLoggedInUsers()

     {

             LinqChatDataContext db = new LinqChatDataContext();


            // let's check if this authenticated user exist in the

           // LoggedInUser table (means user is logged-in to this room)

         var user = (from u in db.LoggedInUsers

                       where u.UserID == Convert.ToInt32(Session["ChatUserID"])

                       && u.RoomID == Convert.ToInt32(lblRoomId.Text)

                       select u).SingleOrDefault();



           // if user does not exist in the LoggedInUser table

           // then let's add/insert the user to the table

             if (user == null)

            {

                 LoggedInUser loggedInUser = new LoggedInUser();

               loggedInUser.UserID = Convert.ToInt32(Session["ChatUserID"]);

                loggedInUser.RoomID = Convert.ToInt32(lblRoomId.Text);

                db.LoggedInUsers.InsertOnSubmit(loggedInUser);

                db.SubmitChanges();

            }

           string userIcon;

           StringBuilder sb = new StringBuilder();



          // get all logged in users to this room

           var loggedInUsers = from l in db.LoggedInUsers

                              where l.RoomID == Convert.ToInt32(lblRoomId.Text)

                               select l;



            // list all logged in chat users in the user list

           foreach (var loggedInUser in loggedInUsers)
          {

               // show user icon based on sex

               if (loggedInUser.Usertab.Sex.ToString().ToLower() == "m")

                    userIcon = "<img src='Images/manIcon.gif' style='vertical-align:middle' alt=''>  ";

                else

                    userIcon = "<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''>  ";



                 if (loggedInUser.Usertab.Username != (string)Session["ChatUsername"])

                        sb.Append(userIcon + "<a href=#>" + loggedInUser.Usertab.Username + "</a><br>");

               else
                   sb.Append(userIcon + "<b>" + loggedInUser.Usertab.Username + "</b><br>");

            }

 

          // holds the names of the users shown in the chatroom

           litUsers.Text = sb.ToString();
      }
    private void InsertMessage(string text)

           {
                LinqChatDataContext db = new LinqChatDataContext();
                Message message = new Message();
                message.RoomID = Convert.ToInt32(lblRoomId.Text);
                message.UserID= Convert.ToInt32(Session["ChatUserID"]);
                if (String.IsNullOrEmpty(text))
                {
                    message.Text = txtMessage.Text.Replace("<", "");
                    message.Color = ddlColor.SelectedValue;
                }
                else
                {
                    message.Text = text;
                    message.Color = "gray";
                }
                message.ToUserID = null;            // in the future, we will use this value for private messages
                message.TimeStamp = DateTime.Now;
                db.Messages.InsertOnSubmit(message);
                db.SubmitChanges();

       }
    private void GetRoomInformation()
    {
        // get the room information from the database
        // we're going to set this up so that we can use
        // many rooms if we want to
        LinqChatDataContext db = new LinqChatDataContext();

        var room = (from r in db.Rooms
                    where r.RoomID == Convert.ToInt32(lblRoomId.Text)
                    select r).SingleOrDefault();

        lblRoomId.Text = room.RoomID.ToString();
        lblRoomName.Text = room.Name;
    }

 
     //this.InsertMessage(ConfigurationManager.AppSettings["ChatLoggedInText"] + " " + DateTime.Now.ToString())
    protected void Page_Load(object sender, EventArgs e)
    
   
   
    {
         if (!IsPostBack)
         {
             ddlbind();
             string roomId = (string)Request["roomId"];
             lblRoomId.Text = roomId;
             this.GetRoomInformation();
             this.GetLoggedInUsers();
             this.InsertMessage(ConfigurationManager.AppSettings["ChatLoggedInText"] + " " + DateTime.Now.ToString());
             this.GetMessages();

             // create a call back reference so we can log-out user when user closes the browser
             string callBackReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "LogOutUser", "");
             string logOutUserCallBackScript = "function LogOutUserCallBack(arg, context) { " + callBackReference + "; }";
             Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "LogOutUserCallBack", logOutUserCallBackScript, true);
         }
    }
    void  System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)

        {

            _callBackStatus = "failed";



           // log out the user by deleting from the LoggedInUser table

    string callBackReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "LogOutUser", "");

                string logOutUserCallBackScript = "function LogOutUserCallBack(arg, context) { " + callBackReference + "; }";

                 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "LogOutUserCallBack", logOutUserCallBackScript, true);       LinqChatDataContext db = new LinqChatDataContext();


            var loggedInUser = (from l in db.LoggedInUsers

                                 where l.UserID == Convert.ToInt32(Session["ChatUserID"])

                              && l.RoomID == Convert.ToInt32(lblRoomId.Text)

                               select l).SingleOrDefault();

           db.LoggedInUsers.DeleteOnSubmit(loggedInUser);

             db.SubmitChanges();



          // insert a message that this user has logged out

           this.InsertMessage("Just logged out! " + DateTime.Now.ToString());



              _callBackStatus = "success";
         }
    
     private void GetMessages()

        {
            LinqChatDataContext db = new LinqChatDataContext();
            var messages = (from m in db.Messages
                            where m.UserID == Convert.ToInt32(Session["ChatUserID"]) && m.RoomID == Convert.ToInt32(lblRoomId.Text)
            orderby m.TimeStamp descending
            select m).Take(20).OrderBy(m => m.TimeStamp);
            if (messages != null)
             {

              StringBuilder sb = new StringBuilder();
              int ctr = 0;    // toggle counter for alternating color
               foreach (var message in messages)

               {

                    // alternate background color on messages

                    if (ctr == 0)

                     {
                       sb.Append("<div style='padding: 10px;'>");

                       ctr = 1;

                   }

                   else

                  {

                        sb.Append("<div style='background-color: #EFEFEF; padding: 10px;'>");
                       ctr = 0;

                   }



                     if (message.Usertab.Sex.ToString().ToLower() == "m")

                        sb.Append("<img src='Images/manIcon.gif' style='vertical-align:middle' alt=''>  " + message.Text + "</div>");

                    else

                    sb.Append("<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''>  " + message.Text + "</div>");
              }


              
             litMessages.Text = sb.ToString();

           }

         }


protected void  BtnSend_Click(object sender, EventArgs e)
     {
         if (txtMessage.Text.Length > 0)
         {
             this.InsertMessages(null);
             this.GetMessages();
             txtMessage.Text = String.Empty;
             ScriptManager1.SetFocus(txtMessage.ClientID);
         }
   
}
private void InsertMessages(string text)
{
    LinqChatDataContext db = new LinqChatDataContext();
    Message message = new Message();
    message.RoomID = Convert.ToInt32(lblRoomId.Text);
    message.UserID = Convert.ToInt32(Session["ChatUserID"]);
    if (String.IsNullOrEmpty(text))
    {
        message.Text = txtMessage.Text.Replace("<", "");
        message.Color = ddlColor.SelectedValue;
    }
    else
    {
        message.Text = text;
        message.Color = "gray";
    }
    message.ToUserID = Convert.ToInt32(DropDownList1.SelectedItem.Value);            // in the future, we will use this value for private messages
    message.TimeStamp = DateTime.Now;
    db.Messages.InsertOnSubmit(message);
    db.SubmitChanges();

}
protected void  Timer1_OnTick(object sender, EventArgs e)
{
    this.GetLoggedInUsers();
    this.GetMessagesddl();

    if ((string)Session["IsChatroomInFocus"] == null)
        ScriptManager1.SetFocus(txtMessage);

}
protected void  BtnLogOut_Click(object sender, EventArgs e)
{
    obj.excquery("delete from Message where UserId=" + Convert.ToInt32(Session["ChatUserID"]) + "");
    obj.excquery("delete from Message where ToUserId=" + Convert.ToInt32(Session["ChatUserID"]) + "");
       LinqChatDataContext db = new LinqChatDataContext();
       var loggedInUser = (from l in db.LoggedInUsers
       where l.UserID == Convert.ToInt32(Session["ChatUserID"])
       && l.RoomID == Convert.ToInt32(lblRoomId.Text)
       select l).SingleOrDefault();
       db.LoggedInUsers.DeleteOnSubmit(loggedInUser);
       db.SubmitChanges();
       // insert a message that this user has logged out
       this.InsertMessage("Just logged out! " + DateTime.Now.ToString());
       // clean the session
        Session.RemoveAll();
        Session.Abandon();
      
       // redirect the user to the login page
        Response.Redirect("Default.aspx");
}
    string  System.Web.UI.ICallbackEventHandler.GetCallbackResult()

         {

            return _callBackStatus;

   }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetMessagesddl();

    }
    private void GetMessagesddl()
    {
        LinqChatDataContext db = new LinqChatDataContext();
        var messages = (from m in db.Messages
                        where m.UserID == Convert.ToInt32(DropDownList1.SelectedItem.Value) && m.ToUserID == Convert.ToInt32(Session["ChatUserID"]) && m.RoomID == Convert.ToInt32(lblRoomId.Text) || m.UserID == Convert.ToInt32(Session["ChatUserID"]) && m.ToUserID == Convert.ToInt32(DropDownList1.SelectedItem.Value) && m.RoomID == Convert.ToInt32(lblRoomId.Text)
                        orderby m.TimeStamp descending
                        select m).Take(20).OrderBy(m => m.TimeStamp);
        if (messages != null)
        {

            StringBuilder sb = new StringBuilder();
            int ctr = 0;    // toggle counter for alternating color
            foreach (var message in messages)
            {

                // alternate background color on messages

                if (ctr == 0)
                {
                    sb.Append("<div style='padding: 10px;'>");

                    ctr = 1;

                }

                else
                {

                    sb.Append("<div style='background-color: #EFEFEF; padding: 10px;'>");
                    ctr = 0;

                }



                if (message.Usertab.Sex.ToString().ToLower() == "m")

                    sb.Append("<img src='Images/manIcon.gif' style='vertical-align:middle' alt=''>  " + message.Text + "</div>");

                else

                    sb.Append("<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''>  " + message.Text + "</div>");
            }



            litMessages.Text = sb.ToString();

        }

    }
}

Wednesday, September 29, 2010

stored procedures

protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
       
       
        string s;
           title = txtTitle.Text.ToString();
         insertcontents.cmd.Parameters.AddWithValue("@title",txtTitle.Text);
         insertcontents.cmd.Parameters.AddWithValue("@url",txtTitle.Text+".html");

         insertcontents.cmd.Parameters.AddWithValue("@bloggerid", Session["idUser"]);
        insertcontents.cmd.Parameters.AddWithValue("@date",DateTime.Now.ToString());
        s = insertcontents.excproc("spInsertTitle");
        if (s == "1")
            Response.Write("Inserted Succ");
       // insertcontents.cmd.Parameters.AddWithValue("@status","Active");
      

    }

ALTER PROCEDURE [dbo].[spInsertTitle]
     (@title varchar(150), @url varchar(50),@bloggerid int,@date varchar(20))
AS

insert into tblTitle values (@title,@url,@bloggerid,@date)

dynamic html

public partial class Blogger_BlogPost : System.Web.UI.Page
{
    DBUpdate insertcontents = new DBUpdate();
    StreamWriter sw;
    string title;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnPublish_Click(object sender, ImageClickEventArgs e)
    {
        sw = File.CreateText(Server.MapPath(txtTitle.Text+".html"));
        sw.WriteLine("<h3>"+txtTitle.Text+"</h3>");
        sw.WriteLine(contentEditor.Content);
        sw.Close();

chatting

http://www.junnark.com/Articles/Build-a-Web-Chat-Application-Using-ASP-Net-LINQ-and-AJAX-CS.aspx

chatroom.aspx.cs

ublic partial class ChatRoom : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
    Class1 obj = new Class1();
    private string _callBackStatus;
   // this.InsertMessage(ConfigurationManager.AppSettings["ChatLoggedInText"] + " " + DateTime.Now.ToString());  
    private void GetLoggedInUsers()

     {

             LinqChatDataContext db = new LinqChatDataContext();


            // let's check if this authenticated user exist in the

           // LoggedInUser table (means user is logged-in to this room)

         var user = (from u in db.LoggedInUsers

                       where u.UserID == Convert.ToInt32(Session["ChatUserID"])

                       && u.RoomID == Convert.ToInt32(lblRoomId.Text)

                       select u).SingleOrDefault();



           // if user does not exist in the LoggedInUser table

           // then let's add/insert the user to the table

             if (user == null)

            {

                 LoggedInUser loggedInUser = new LoggedInUser();

               loggedInUser.UserID = Convert.ToInt32(Session["ChatUserID"]);

                loggedInUser.RoomID = Convert.ToInt32(lblRoomId.Text);

                db.LoggedInUsers.InsertOnSubmit(loggedInUser);

                db.SubmitChanges();

            }

           string userIcon;

           StringBuilder sb = new StringBuilder();



          // get all logged in users to this room

           var loggedInUsers = from l in db.LoggedInUsers

                              where l.RoomID == Convert.ToInt32(lblRoomId.Text)

                               select l;



            // list all logged in chat users in the user list

           foreach (var loggedInUser in loggedInUsers)
          {

               // show user icon based on sex

               if (loggedInUser.Usertab.Sex.ToString().ToLower() == "m")

                    userIcon = "<img src='Images/manIcon.gif' style='vertical-align:middle' alt=''>  ";

                else

                    userIcon = "<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''>  ";



                 if (loggedInUser.Usertab.Username != (string)Session["ChatUsername"])

                        sb.Append(userIcon + "<a href=#>" + loggedInUser.Usertab.Username + "</a><br>");

               else
                   sb.Append(userIcon + "<b>" + loggedInUser.Usertab.Username + "</b><br>");

            }

 

          // holds the names of the users shown in the chatroom

           litUsers.Text = sb.ToString();
      }
    private void InsertMessage(string text)

           {
                LinqChatDataContext db = new LinqChatDataContext();
                Message message = new Message();
                message.RoomID = Convert.ToInt32(lblRoomId.Text);
                message.UserID= Convert.ToInt32(Session["ChatUserID"]);
                if (String.IsNullOrEmpty(text))
                {
                    message.Text = txtMessage.Text.Replace("<", "");
                    message.Color = ddlColor.SelectedValue;
                }
                else
                {
                    message.Text = text;
                    message.Color = "gray";
                }
                message.ToUserID = null;            // in the future, we will use this value for private messages
                message.TimeStamp = DateTime.Now;
                db.Messages.InsertOnSubmit(message);
                db.SubmitChanges();

       }
    private void GetRoomInformation()
    {
        // get the room information from the database
        // we're going to set this up so that we can use
        // many rooms if we want to
        LinqChatDataContext db = new LinqChatDataContext();

        var room = (from r in db.Rooms
                    where r.RoomID == Convert.ToInt32(lblRoomId.Text)
                    select r).SingleOrDefault();

        lblRoomId.Text = room.RoomID.ToString();
        lblRoomName.Text = room.Name;
    }

 
     //this.InsertMessage(ConfigurationManager.AppSettings["ChatLoggedInText"] + " " + DateTime.Now.ToString())
    protected void Page_Load(object sender, EventArgs e)
    
   
    {
         if (!IsPostBack)
         {
             string roomId = (string)Request["roomId"];
             lblRoomId.Text = roomId;
             this.GetRoomInformation();
             this.GetLoggedInUsers();
       this.InsertMessage(ConfigurationManager.AppSettings["ChatLoggedInText"] + " " + DateTime.Now.ToString());
             this.GetMessages();

             // create a call back reference so we can log-out user when user closes the browser
             string callBackReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "LogOutUser", "");
             string logOutUserCallBackScript = "function LogOutUserCallBack(arg, context) { " + callBackReference + "; }";
             Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "LogOutUserCallBack", logOutUserCallBackScript, true);
         }
    }
    void  System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)

        {

            _callBackStatus = "failed";



           // log out the user by deleting from the LoggedInUser table

    string callBackReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "LogOutUser", "");

                string logOutUserCallBackScript = "function LogOutUserCallBack(arg, context) { " + callBackReference + "; }";

                 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "LogOutUserCallBack", logOutUserCallBackScript, true);       LinqChatDataContext db = new LinqChatDataContext();


            var loggedInUser = (from l in db.LoggedInUsers

                                 where l.UserID == Convert.ToInt32(Session["ChatUserID"])

                              && l.RoomID == Convert.ToInt32(lblRoomId.Text)

                               select l).SingleOrDefault();

           db.LoggedInUsers.DeleteOnSubmit(loggedInUser);

             db.SubmitChanges();



          // insert a message that this user has logged out

           this.InsertMessage("Just logged out! " + DateTime.Now.ToString());



              _callBackStatus = "success";
         }
    
     private void GetMessages()

        {
            LinqChatDataContext db = new LinqChatDataContext();
            var messages = (from m in db.Messages
                            where m.RoomID == Convert.ToInt32(lblRoomId.Text)
            orderby m.TimeStamp descending
            select m).Take(20).OrderBy(m => m.TimeStamp);
            if (messages != null)
             {

              StringBuilder sb = new StringBuilder();
              int ctr = 0;    // toggle counter for alternating color
               foreach (var message in messages)

               {

                    // alternate background color on messages

                    if (ctr == 0)

                     {
                       sb.Append("<div style='padding: 10px;'>");

                       ctr = 1;

                   }

                   else

                  {

                        sb.Append("<div style='background-color: #EFEFEF; padding: 10px;'>");
                       ctr = 0;

                   }



                     if (message.Usertab.Sex.ToString().ToLower() == "m")

                        sb.Append("<img src='Images/manIcon.gif' style='vertical-align:middle' alt=''>  " + message.Text + "</div>");

                    else

                    sb.Append("<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''>  " + message.Text + "</div>");
              }


              
             litMessages.Text = sb.ToString();

           }

         }


protected void  BtnSend_Click(object sender, EventArgs e)
     {
         if (txtMessage.Text.Length > 0)
         {
             this.InsertMessages(null);
             this.GetMessages();
             txtMessage.Text = String.Empty;
             ScriptManager1.SetFocus(txtMessage.ClientID);
         }
   
}
private void InsertMessages(string text)
{
    LinqChatDataContext db = new LinqChatDataContext();
    Message message = new Message();
    message.RoomID = Convert.ToInt32(lblRoomId.Text);
    message.UserID = Convert.ToInt32(Session["ChatUserID"]);
    if (String.IsNullOrEmpty(text))
    {
        message.Text = txtMessage.Text.Replace("<", "");
        message.Color = ddlColor.SelectedValue;
    }
    else
    {
        message.Text = text;
        message.Color = "gray";
    }
    message.ToUserID = null;            // in the future, we will use this value for private messages
    message.TimeStamp = DateTime.Now;
    db.Messages.InsertOnSubmit(message);
    db.SubmitChanges();

}
protected void  Timer1_OnTick(object sender, EventArgs e)
{
    this.GetLoggedInUsers();
    this.GetMessages();

    if ((string)Session["IsChatroomInFocus"] == null)
        ScriptManager1.SetFocus(txtMessage);

}
protected void  BtnLogOut_Click(object sender, EventArgs e)
{
       LinqChatDataContext db = new LinqChatDataContext();
       var loggedInUser = (from l in db.LoggedInUsers
       where l.UserID == Convert.ToInt32(Session["ChatUserID"])
       && l.RoomID == Convert.ToInt32(lblRoomId.Text)
       select l).SingleOrDefault();
       db.LoggedInUsers.DeleteOnSubmit(loggedInUser);
       db.SubmitChanges();
       // insert a message that this user has logged out
       this.InsertMessage("Just logged out! " + DateTime.Now.ToString());
       // clean the session
        Session.RemoveAll();
        Session.Abandon();
       // redirect the user to the login page
        Response.Redirect("Default.aspx");
}
    string  System.Web.UI.ICallbackEventHandler.GetCallbackResult()

         {

            return _callBackStatus;

   }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //obj.gettable("select * from Usertab where UserID=" + DropDownList1.SelectedItem.Value + "");
        //if(obj.dt.Rows.Count>0)
        //{

        //}

    }
}
        

Tuesday, September 28, 2010

create xml dynamically

public partial class User_userHome : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings.Get("connString"));
    SqlCommand cmd;
    SqlDataReader dr,drdr;
    DBUpdate userNews = new DBUpdate();
   
    protected void Page_Load(object sender, EventArgs e)
    {
        string name = "";
              
        int idUser = Convert.ToInt32(Session["idUser"]);

        userNews.cmd.Parameters.Clear();

        SqlParameter p = new SqlParameter("@ret", SqlDbType.Int);
        p.Direction = ParameterDirection.ReturnValue;
        userNews.cmd.Parameters.Add(p);
        userNews.cmd.Parameters.AddWithValue("@userid", idUser);

        string fetchrow = "select * from userRegistration,tblUser where userRegistration.userId = tblUser.registrationid and  tblUser.userid = " + idUser;

        drdr = userNews.datareader(fetchrow);
        while (userNews.dr.Read())
        {

            name = drdr["firstname"].ToString();
        }
        lblWelcome.Text = "Welcome " + name;
     
    }



    protected void dnldBtn_Click(object sender, ImageClickEventArgs e)
    {

        XmlTextReader reader = null;

        try
        {

            reader = new XmlTextReader("D:\\Lis\\RssNews\\User\\RssXml.xml");

            DataSet ds = new DataSet();

            ds.ReadXml(reader);
            //GridView1.DataSource = ds.Tables["item"];
            //GridView1.DataBind();

            Response.ContentType = "application/octet-stream";
            Response.ContentType = "application/x-download";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + "RssXml.xml");


            Response.WriteFile(Server.MapPath("RssXml.xml"));
            Response.End();


        }

        catch (Exception ex)
        {

            lblMessage.Text = ex.Message;

        }

        finally
        {

            reader.Close();

        }

    }
    protected void btnRss_Click(object sender, ImageClickEventArgs e)
    {
        String strFilePath = "D:\\Lis2008\\RssFeeds\\User\\RssXml.xml";


        XmlTextWriter writer = new XmlTextWriter(strFilePath, Encoding.UTF8);
        writer.WriteStartDocument();
        writer.WriteStartElement("rss");
        writer.WriteAttributeString("version", "2.0");
        writer.WriteString("\n\n");
        writer.WriteStartElement("channel");
        writer.WriteString("\n\n");
        userNews.getcon();
        cmd = new SqlCommand("select * from tblNews", con);
        cmd.Connection = userNews.con;
        dr = cmd.ExecuteReader();
        int cnt = 1;
        while (dr.Read())
        {
            if (cnt == 1)
            {
                writer.WriteString("\n");
                writer.WriteElementString("title", dr["Title"].ToString());
                writer.WriteString("\n");
                writer.WriteElementString("description", dr["description"].ToString());
                writer.WriteString("\n");
                writer.WriteElementString("link", dr["link"].ToString());
                writer.WriteString("\n");

            }
            else
            {
                writer.WriteStartElement("item");
                writer.WriteString("\n");
                writer.WriteElementString("title", dr["title"].ToString());
                writer.WriteString("\n");
                writer.WriteElementString("description", dr["description"].ToString());
                writer.WriteString("\n");
                writer.WriteElementString("link", dr["link"].ToString());
                writer.WriteString("\n");
                writer.WriteEndElement();
                writer.WriteString("\n");
            }
            cnt = cnt + 1;
        }


        con.Close();


        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.Flush();

        writer.Close();
        Response.Redirect("RssXml.xml");
    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        //     string gender;

        //    string firstname = txtFirstName.Text.ToString();
        //    string lastname = txtLastName.Text.ToString();
        //    string username = txtUsername.Text.ToString();
        //    string password = txtPassword.Text.ToString();
        //    string address = txtAddress.Text.ToString();
        //    string mobile = txtMobile.Text.ToString();
        //    string phone = txtPhone.Text.ToString();
        //    string email = txtEmail.Text.ToString();

        //    if (rdmale.Checked)
        //    {
        //        gender = "M";
        //    }
        //    else gender = "F";
        //     string status = "Active";
        //     userNews.cmd.Parameters.Clear();

        //     SqlParameter p = new SqlParameter("@ret", SqlDbType.Int);
        //     p.Direction = ParameterDirection.ReturnValue;
        //     userNews.cmd.Parameters.Add(p);
        //     userNews.cmd.Parameters.AddWithValue("@firstname", txtFirstName.Text);
        //     userNews.cmd.Parameters.AddWithValue("@lastname", txtLastName.Text);
        //     userNews.cmd.Parameters.AddWithValue("@username", txtUsername.Text);
        //     userNews.cmd.Parameters.AddWithValue("@password", txtPassword.Text);
        //     userNews.cmd.Parameters.AddWithValue("@address", txtAddress.Text);
        //     userNews.cmd.Parameters.AddWithValue("@mobile", txtMobile.Text);
        //     userNews.cmd.Parameters.AddWithValue("@phone", txtPhone.Text);
        //     userNews.cmd.Parameters.AddWithValue("@email", txtEmail.Text);
        //     userNews.cmd.Parameters.AddWithValue("@gender", gender);
        //     userNews.cmd.Parameters.AddWithValue("@status", status);

        //    ccJoin.ValidateCaptcha(txtCaptcha1.Text);
        //      if (ccJoin.UserValidated)
        //     {
        //         string s = userNews.excproc("userinsert");
        //         clientScriptAlert("Registered Successfully");
        //     }
        //     else
        //         return;
        //}
        //private void clientScriptAlert(string Message)
        //{
        //    Page.RegisterClientScriptBlock("clientScript", "<script>alert('" + Message + "')</script>");
        }

    }


   

ABC's OF WCF

  1. Address [the where]
    Defines where on the network messages should be sent so that the endpoint receives them.
    This is the location to which messages must be sent by the client.
    For HTTP, the address http://myserver/myservice/
    For TCP, the address net.tcp://myserver:8080/myservice
    Address is where you communicate. This is not the same as the location you deploy your service, but the URL that will be used internally to map your requests and responses.
  2. Binding [The How]
    Defines the channel used to communicate with an endpoint.
    Channels are the conduit through which all messages pass within a WCF application.
    A channel is composed of a series of binding elements.
    The lowest level binding element is the transport.
    The built-in transports include HTTP, TCP, Named Pipes, PerChannel, and MSMQ. Above this are binding elements that specify security and transactions.
    Fortunately, WCF ships with system-provided bindings that have the channels stacked and configured correctly to save you the time of figuring it out yourself.
    Which delivers messages over the network.
    Binding is how you communicate. There are several default ones like BasicHttp, TCP, NamedPipes, MSMQ and several others. This is the protocol that the server and client understands while communicating.

  3. Contract [The What]
    Defines the capability or feature set offered by the endpoint.
    Defines the operations that an endpoint exposes and the message formats that the operations require.
    Contract operations map to class methods that implement the endpoint, including the signature of parameters passed in and out of the methods.
    Contract is what you communicate.
  4. There are two types of contracts:
    Service Contracts:
    It is the API the service consumer invokes on your service. It's the method signature that will go into the WSDL.
    Data Contracts:
    It is your data that would travel from the service consumer and the service.
    It's the data structure.
    This can be found in the schema of your service.
    There is something else called Message Contract, which is basically a way to communicate using messages instead of strictly typed structures. There are several situations where you want to use this, but is out of the context for this Session.