ADO NET

jQuery UI Accordion - Default functionality

Deleting Record using SQLDataAdapter without using SQLCommandBuilder

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection connection ;
            SqlDataAdapter adapter = new SqlDataAdapter();
            string sql = null;
            connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
            connection = new SqlConnection(connetionString);
            sql = "delete product where Product_name ='Product6'";
            try
            {
                connection.Open();
                adapter.DeleteCommand = connection.CreateCommand();
                adapter.DeleteCommand.CommandText = sql;
                adapter.DeleteCommand.ExecuteNonQuery();
                MessageBox.Show ("Row(s) deleted !! ");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}


Uisng SQLCommandBuilder to perform DML operations.


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;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=master;Integrated Security=True");
    DataTable dt = new DataTable();
    SqlDataAdapter da;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            da = new SqlDataAdapter("select * from tb_Student", con);
            SqlCommandBuilder cb = new SqlCommandBuilder(da);
            da.Fill(dt);
        }

    }

    protected void btnRegister_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (txtRollNo.Text == dt.Rows[i]["RollNO"].ToString())
            {
                Response.Write("<script>alert('Roll No Already Exists')</script>");
                return;

            }
        }

        DataRow dr = null;
        dr = dt.NewRow();
        dr["RollNO"] = txtRollNo.Text;
        dr["Name"] = txtStuName.Text;
        dr["Class"] = txtClass.Text;
        dr["Section"] = txtSection.Text;
        dr["Age"] = txtAge.Text;
        dt.Rows.Add(dr);
        da.Update(dt);
      
        Response.Write("<script>alert('Student Added Successfully')</script>");
     
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        int k = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (txtRollNo.Text == dt.Rows[i]["RollNO"].ToString())
            {
                k = 1;
                DataRow dr = null;
                dr = dt.Select("RollNo=" + txtRollNo.Text)[0];
                if (dr != null)
                {
                    dr.Delete();
                    da.Update(dt);
                    Response.Write("<script>alert('Deleted Successfully')</script>");
                 
                    break;
                }

            }
          
        }
        if (k == 0)
        {
            Response.Write("<script>alert('There is no such RollNO to Delete')</script>");
    
        }
       
      
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        int k = 0;
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (txtRollNo.Text == dt.Rows[i]["RollNO"].ToString())
            {
                k = 1;
                DataRow dr = null;
                dr = dt.Select("RollNo=" + txtRollNo.Text)[0];
                if (dr != null)
                {

                    dr.BeginEdit();
                    dr["RollNO"] = txtRollNo.Text;
                    dr["Name"] = txtStuName.Text;
                    dr["Class"] = txtClass.Text;
                    dr["Section"] = txtSection.Text;
                    if (txtAge.Text != "")
                    {
                        dr["Age"] = (txtAge.Text);
                    }
                    dr.EndEdit();
                    int res = da.Update(dt);
                    if (res > 0)
                    {
                        Response.Write("<script>alert('Updated Successfully')</script>");
                    }
                    break;
                }

            }
        }

        if (k == 0)
        {
            Response.Write("<script>alert('There is no such Roll No available')</script>");

        } 


    }
}

What is a Transaction?

fsdfsdfsdfdfdfdf

No comments:

Post a Comment