Posted by : Sudhir Chekuri Saturday, 12 March 2016



SQL Command to create SQL table

CREATE TABLE [dbo].[Tbl_Data](
       [Id] [int] IDENTITY(1,1) NOT NULL,
       [UserName] [varchar](50) NULL,
       [EmailId] [varchar](50) NULL,
       [Password] [varchar](50) NULL,
PRIMARY KEY CLUSTERED
(
       [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

------------

ADO.NET C# code to insert data into SQL database table and to view the data
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }

        protected void Btn_Insert_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-R72EIR5\SQLEXPRESS;Initial Catalog=mk;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("insert Tbl_Data(Username,EmailId,Password) Values('" + Txt_UserName.Text + "','" + Txt_EmailId.Text + "','" + Txt_Password.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Txt_UserName.Text = "";
            Txt_EmailId.Text = "";
            Txt_Password.Text = "";
            Response.Write("inserted Successfully");

        }

        protected void Btn_Select_Click1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-R72EIR5\SQLEXPRESS;Initial Catalog=mk;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("Select * From Tbl_Data", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GV_Select.DataSource = ds;
            GV_Select.DataBind();

        }
    }
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -