Sunday, July 8, 2012

Delete from SQL using c# ASP.Net

Hello friends. This is My Second Post
Delete Record from SQL 2005 using C# code and ASP.Net web Application


 To view the Insert coding and other details please follow MY this Link.


http://sandeepraojabalpur.blogspot.in/2012/07/insert-querry-in-sql-database-using.html




In This  Section I am only writing the Delete Coding.





using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlDataReader dr;
    SqlCommand cm;
    SqlConnection cn;
    protected void Page_Load(object sender, EventArgs e)
    {

    }



protected void Button2_Click(object sender, EventArgs e)
    {
        string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
        cn = new SqlConnection(cs);
        cn.Open();
        string k = "delete from Table1 where roll='" + TextBox1.Text + "'";
        cm = new SqlCommand(k, cn);
        cm.ExecuteNonQuery();
        Response.Write("Data Deleted from Database");
        TextBox1.Text = "";
        TextBox2.Text = "";
   
    }

Insert Querry in SQL Database using c#,asp.net 2.0 in only 6 Lines

Hai..Friends. My Name is Sandeep Rao .Am a Website Developer.

In this session I will show you the coding How to insert record in Database SQL Server 2005 using

C# and ASP.Net 2.0.    in 6 lines


Step1: Open Asp.net  web application. You will see a Default.aspx page.
Step2: Now take two TextBox from Toolbox and take a Button.
Step3: Now go to top menu and go to Website-->Add New Item-->SQLDataSource-->(Add it).
           Do not rename the database. Click YES when it ask to place it in APP_Data folder.
Step4: Go to Database Explorer. Click on Table-->Rightclick-->Add New Table.
Step5: A form will open. Enter field name.
  Let say         roll       nvarchar(MAX)
                      name    nvarchar(MAX)

now click save.
Do not rename the table name...Let it be " Table1"..You can rename it afterwards.

Step6:  Now lets start coding. Double Click on Button1. You will reach a coding part.
something like this


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;         

public partial class _Default : System.Web.UI.Page
{
    

    
    protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)
    {
     }


Step7: If you see the above code..You were going in right direction.Else there might something wrong
in the Steps.


Now write this. Please check every line.
NOTE
The way to find connection string is to go to Database Explorer--->Select Table--> and drag it to your webpage... It will become GridView. Now with Gridview there would be a BOX named SQL datasource. At top right corner of Box you will see a small arrow.Click on it. Go to Configure Datasource and Click + sign.Here you will find connection string. Copy it and paste it in your codeing. Now delete that Gridview and box.
NOTE
The control name is given between   single cote   double cote + Controlname+double cote singlecote comma
Example   ' "+TextBox1.Text+" ',

Now lets Start Coding


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;   //WRITE THIS

public partial class _Default : System.Web.UI.Page
{
     SqlDataReader dr;   //WRITE THIS
    SqlCommand cm;      //WRITE THIS
    SqlConnection cn;    //WRITE THIS

    
    protected void Page_Load(object sender, EventArgs e)

{

}
protected void Button1_Click(object sender, EventArgs e)
    {

        //Insert Coding //WRITE THIS

        string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
        cn = new SqlConnection(cs);
        cn.Open();
        string k = "insert into Table1 values('"+TextBox1.Text+"','"+TextBox2.Text+"')";
        cm = new SqlCommand(k, cn);
        cm.ExecuteNonQuery();
        TextBox1.Text = "";
        TextBox2.Text = "";
        
        Response.Write("Data Saved");

     }




========
The above coding is for Insert.
Now Run the Project.Enter Roll and name and click on Button.Your Data is saved in Database...


Thanks
Regard
Sandeep Rao

Please comment if you Like it