Friday, November 27, 2015

Up-Date record in SQL Database using C#.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            SqlConnection cnn = new SqlConnection(@"Data Source=localhost\SQLExpress;Initial Catalog=awais;Integrated Security=True;Integrated Security=True ");
            cnn.Open();
string query = "update logout set name ='"+TextBox3.Text+"' where name = '" + TextBox2.Text + "';";
            SqlCommand cmd = new SqlCommand(query, cnn);
            cmd.ExecuteNonQuery();

            Response.Write("update  is done!");

        }

   
    }
}


Friday, November 20, 2015

Retrieve / Collect data from SQL database using c#.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

 
        protected void Button2_Click(object sender, EventArgs e)
        {

            try
            {
                SqlConnection cnn = new SqlConnection(@"Data Source=localhost\SQLExpress;Initial Catalog=awais;Integrated Security=True;Integrated Security=True ");
                cnn.Open();
                SqlCommand command = new SqlCommand("SELECT name,password from logout;", cnn);


                SqlDataReader reader = command.ExecuteReader();

                if (reader.HasRows)
                {
                    Response.Write("<table border=\"3\">");
                    while (reader.Read())
                    {
                        Response.Write("<tr><th width=\"40\">"+ reader.GetString(0) +"</th><th>"+ reader.GetString(1)+"</th></tr>");
                    } Response.Write("</table>");
                }
                else
                {
                    Response.Write("No rows found.");
                }
                reader.Close();
            }
            catch (SystemException ex) { Response.Write(ex); }
           
        }
    }
}