Thursday 13 February 2014

delete record in database(one tire)

your database like
    database name :student
    table name        :tbl_StudentData
    Field name         :id,name,age,gender,city,phone

Your .aspx page
         
------------------------------------------------------------------------------------------------

         

<div>
    Enter ID for delete record :
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br/>

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Delete" />
</div>


--> Copy following code in your .aspx.cs file

--> if you want to delete record in table or database that time add following namespace in your code behind file
------------------------------------------------------------------------------------------------



using System.Data;
using System.Data.SqlClient;


--> and then create following  object above page load event
------------------------------------------------------------------------------------------------

     SqlConnection cn;

    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;


--> next assign the connection string in page laod event
------------------------------------------------------------------------------------------------

  

cn = new SqlConnection(@"DataSource=\student.mdf;Integrated Security=True;User Instance=True");


--> and then write following code in your code behind on click event
------------------------------------------------------------------------------------------------

        int i = Convert.ToInt32(TextBox1.Text);
        cmd = new SqlCommand("delete from tbl where id='" + i + " '", cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();

No comments:

Post a Comment



Asp.net tutorials