1. Open SQL Server management studio and
create database named TEST. Then add a table with two columns named name and
fathername. we'll add data into name and fathername using asp.net.
2. Now open visual studio and create new website. Then add new webform.
3. Now add two text boxes and a button from toolbox.
4. Double click on button .it'll open codebehind file of webform.
5. Now we have to add connection string of the database. Save your connection
string in web.config file
<configuration>
<connectionStrings>
<add name="myconectionstring"
connectionString="data source=.\SQLEXPRESS;initial catalogue=your database name;Integrated Security=SSPI";providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="false" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
6. Now we'll get this connection string in our codebehind file.
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
7. Now we'll create sqlconnection .Add namespace using System.data.sqlclient at top.Now write following code in button click event.
string cs=System.Configuration.ConfigurationManager.ConnectionStrings["myconectionstring"].ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("INSERTINTO TEST (name,fathername) VALUES('" + TextBox1.Text + "','" + TextBox1.Text + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
8. Its done.
Shobhit SaxenaPosted Mar 16, 2017, 3:09 AM
Try{ SqlConnection con=new SqlConnection();con.ConnectionString=ConfigurationManager.ConnectionStrings['con1'].ToString();SqlDataAdapter adp=new SqlCommand("insert into abc (name,fname) values('" + TextBox1.Text + "','" + TextBox1.Text + "')", con);label1.Text="inserted successfully';}catch(Exception e1) Label1.Text=e1.Message();}
Manoj RudrakarPosted May 7, 2015, 3:58 PM
Thanks but I got an error "the name 'textbox id'does not exist in the current context"
palani velaPosted Feb 19, 2015, 6:10 AM
thanks
Muhammad younasPosted Jul 31, 2013, 6:20 AM
you can save your connection string in web.config file . so you can use it on any page without writing it again and again.and one more advantage is that when you have to change conection string, go to web config file and change it there only. otherwise you have to change it gain and again
Rosi Sreenivasa ReddyPosted Jul 31, 2013, 3:25 AM
thank u
Rosi Sreenivasa ReddyPosted Jul 31, 2013, 3:25 AM
i got with this ok,
Rosi Sreenivasa ReddyPosted Jul 31, 2013, 3:24 AM
Connection String="data source=WEBDEV1-PC; Initial Catalog=srinu ; user Id=sa; password=pftec;"
Rosi Sreenivasa ReddyPosted Jul 31, 2013, 3:08 AM
protected void btn_save(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=WEBDEV1-PC;Initial Catalog=srinu;User Id=sa;Password=Pftec"); SqlCommand cmd = new SqlCommand("insert into Test12213 values('" textBox1.Text "','" textBox2.Text "')", con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); }