Hii friends today i am going show you how to insert the data into the Database, which we have created earlier. learn how to create Database and table in SQL Server. & learn how to connect that database with the SQL Server.
Step-1 :- open visual studio , File→New→Website this window will be open right click on website name.
Step-2 :- Right Click On Website Name-Add New Item.
Step-3 :- Add New Item→Web Form & Double click on it OR simply click on add.
Step-5 :- This is design part go to the toolbox if there is no toolbox then go to View→ Toolbox.
Step-6 :- In Toolbox we have tools to design like making of login page or registration page just drag & drop whatever you need.
Step-7 :- Set the ID of text-boxes & buttons & label as well by right click→properties.
Step-8 :- After clicking on the property go to ID section & change the ID.
Step-9 :- Now go to server Explorer→Click on your Database Name Which you hsve created in SQL Server management studio →Properties .
Step-10 :- Copy the Connection String Path.
Step- 11 :- Paste it in "web.config" page.
Step-12 :- paste it in Under the <configuration >
<connectionStrings>
<add name="con" connectionString="Data Source=(local);Initial Catalog=vishmyblog;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Note :- paste it of your database connection String only.
Step-13 :- Now Go To "default.aspx" page or design page just click on button , when you click on button then "default.aspx.cs" page will be open in which the code will be the by default.
Step-14 :-Now write the C# code add three namespace here.
Code :-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int roll = Convert.ToInt32(txt1.Text);
string name = txt2.Text;
string sclass = txt3.Text;
string addr = txt4.Text;
try
{
string url = ConfigurationManager.ConnectionStrings["con"].ToString();
SqlConnection con = new SqlConnection(url);
con.Open();
string query = "insert into tbl_vishmyblog1 values('"+roll +"','"+name +"','"+sclass +"','"+addr +"')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
lbl.Text = "Record Inserted Successfully !!!";
txt1.Text = "";
txt2.Text = "";
txt3.Text = "";
txt4.Text = "";
txt1.Focus();
con.Close();
}
catch (Exception ex)
{
lbl.Text = "Error :" + ex.Message;
}
}
protected void TextBox4_TextChanged(object sender, EventArgs e)
{
}
}
Step-15 :- Now run The Code by clicking on play button.
Step-16 :- Now you can see your output on browser & Enter the data then click on Submit.
Step- 17 :- Now you can see your data is inserted successfully into the database.
Note :- practice it at home.
thank you!!!
0 Comments