Sunday 25 November 2012

Asp.net config file for upload datas in sql db

webconfig file:
<connectionStrings>
    <add name="arunkumar" connectionString="Data Source=AAGNA-PC\SQLEXPRESS;Initial Catalog=giri;Integrated Security=true;User id=sa;Password=;"/>
  </connectionStrings>

in public class file:
static string strcon = ConfigurationManager.ConnectionStrings["arunkumar"].ConnectionString;
    SqlConnection con = new SqlConnection(strcon);

button_click coding:
 protected void Button1_Click(object sender, EventArgs e)
    {
        //SqlConnection con = new SqlConnection("data source=AAGNA-PC;I ntegrated Security=SSPI;Initial Catalog:tb_login ;User Instance=true;");
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into tb_login(username,password) values('"+ txt.Text  +"','"+ txt1.Text +"') ", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }


button_click for grid view display:
protected void Button2_Click(object sender, EventArgs e)
    {
        SqlCommand cmd1 = new SqlCommand("select * from tb_login",con);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(cmd1);
        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }

database sql coding :
after creating database named giri after workout this query !

USE [giri]
GO
/****** Object:  Table [dbo].[tb_login]    Script Date: 11/26/2012 12:30:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tb_login](
    [username] [varchar](50) NULL,
    [password] [varchar](50) NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

output :


No comments:

Post a Comment