Archive for March 2016

Stored Procedures in ADO.NET

// 1. create a command object identifying
        // the stored procedure
        SqlCommand cmd  = new SqlCommand(
               "CustOrderHist", conn);
 
        // 2. set the command object so it knows
        // to execute a stored procedure
        cmd.CommandType = CommandType.StoredProcedure;
 
        // 3. add parameter to command, which
        // will be passed to the stored procedure
        cmd.Parameters.Add(
               new SqlParameter("@CustomerID", custId));



Saturday, 12 March 2016
Posted by Sudhir Chekuri
Tag :

Disconnected Architecture Delete

//Disconnected architecture - delete
        //Delete product
        private void BtnDeleteProduct_Click(object sender, EventArgs e)
        {
             //SqlCommand to delete Product
            SqlCommand cmd = new SqlCommand("exec Usp_DeleteProduct '"+CBExistingCategories.Text+"','"+CBExistingProducts.Text+"'", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            //Update Products
            PopulateProducts();
            //Clear product details
            TxtDescription.Text = "";
            NudPrice.Value = 1;
                MessageBox.Show("Product Deleted Successfully");
           

        }


Posted by Sudhir Chekuri
Tag :

Disconnected Architecture Update

//Disconnected - update
        //Update product name based on the product name under a category
        private void BtnUpdateProduct_Click(object sender, EventArgs e)
        {
            //SqlCommand to delete Product
            SqlCommand cmd = new SqlCommand("exec Usp_UpdateProduct '" + CBExistingCategories.Text + "','" + CBExistingProducts.Text + "','"+TxtUpdateProduct.Text+"'", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            //Update Products
            PopulateProducts();
            //Show first item
            CBExistingProducts.SelectedIndex = 0;
            MessageBox.Show("Product Updated Successfully");
          
        }
        //Disconnected Update
        //Update product details
        private void BtnAddDetails_Click(object sender, EventArgs e)
        {
            //exec Usp_AddProductDetails 'Drinks','Sprites','200 ml sprite chota bottle',25
            //SqlCommand to delete Product
            SqlCommand cmd = new SqlCommand("exec Usp_AddProductDetails '" + CBExistingCategories.Text + "','" + CBExistingProducts.Text + "','" + TxtDescription.Text + "',"+NudPrice.Value+"", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            MessageBox.Show("Product details added Successfully");

        }


Posted by Sudhir Chekuri
Tag :

Disconnected Architecture Insert

//Disconnected Insert
        //Adding product
        private void BtnAddProduct_Click(object sender, EventArgs e)
        {
SqlConnection con =new SqlConnection (@"Data Source=DESKTOP-LEBFFH4\PRESS;Initial Catalog=STUDENT;Integrated Security=True");
            //SqlCommand to insert Product
            SqlCommand cmd = new SqlCommand("exec Usp_InsertProduct '" + CBExistingCategories.Text + "','" + TxtAddProduct.Text + "','" + TxtDescription.Text + "'," + NudPrice.Value+ "", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
                MessageBox.Show("Product Added Successfully");
         

        }



Posted by Sudhir Chekuri
Tag :

Disconnected Architecture Select

//Disconnected - Select
        //Populate the datagridview with registered users information
        public void GetData()
        {
SqlConnection con =new SqlConnection (@"Data Source=DESKTOP-LEBFFH4\PRESS;Initial Catalog=STUDENT;Integrated Security=True");
            //SqlCommand to insert Category
            SqlCommand cmd = new SqlCommand("select * from Tbl_Register", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds, "Users");
          
            //Display users table in DataGridView
            DGVUsers.DataSource = ds.Tables["Users"];
        }
//Disconnected - select
        //Populate Products
        public void PopulateProducts()
        {
SqlConnection con =new SqlConnection (@"Data Source=DESKTOP-LEBFFH4\PRESS;Initial Catalog=STUDENT;Integrated Security=True");
            //SqlCommand to get products
            SqlCommand cmd = new SqlCommand("exec Usp_GetProducts '"+CBExistingCategories.Text+"'", con);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds,"Products");
            //Clear existing Products
            CBExistingProducts.Items.Clear();
            //Adding Products to combobox
           
                for(int i=0;i<ds.Tables["Products"].Rows.Count;i++)
                {
                    //Add categories to Combobox
                    CBExistingProducts.Items.Add(ds.Tables["Products"].Rows[i]["ProductName"]);
                }

        }



Posted by Sudhir Chekuri
Tag :

DataSet

DataSet is used in disconnected architecture to store database tables. It can hold multiple tables in it. It is used to store information retrieved by DataAdapter in disconnected architecture. Data adapter will execute the command and fill the dataset with the data retrieved by execution of SQL command.

Posted by Sudhir Chekuri
Tag :

DataAdapter

DataAdapter is used to disconnected architecture to execute all the commands. It is capable of opening the connection to the database server, executing the command, Storing data in DataSet and closing the connection.


Posted by Sudhir Chekuri
Tag :

Connected Architecture Delete

//Connected architecture - delete
        //Delete Category
        private void BtnDeleteCategory_Click(object sender, EventArgs e)
        {

            //SqlCommand to delete Category
            SqlCommand cmd = new SqlCommand("exec Usp_DeleteCategory '" + CBExistingCategories.Text + "'", con);
            //Open connection
            con.Open();
            //Execute delete command
            int i = cmd.ExecuteNonQuery();
            con.Close();

            if (i > 0)
            {
                //Refresh Category list
                PopulateCategories();
                MessageBox.Show("Category Deleted Successfully");

            }
            else { MessageBox.Show("Category Not Deleted"); }

        }


Posted by Sudhir Chekuri
Tag :

Connected Architecture Update

//Connected Architecture - update
        //Update Category
        private void BtnUpdateCategory_Click(object sender, EventArgs e)
        {
            //SqlCommand to update Category
            SqlCommand cmd = new SqlCommand("exec Usp_UpdateCategory '"+CBExistingCategories.Text+"','"+TxtUpdateCategory.Text+"'", con);
            //Open connection
            con.Open();
            //Execute update command
            int i = cmd.ExecuteNonQuery();
            con.Close();

            if (i > 0)
            {
                //Refresh Category list
                PopulateCategories();
                //Show first Category
                CBExistingCategories.SelectedIndex = 0;
                MessageBox.Show("Category Updated Successfully");

            }
            else { MessageBox.Show("Category Not Updated"); }
           
        }


Posted by Sudhir Chekuri
Tag :

Connected Architecture Insert and Select



SQL Command to create SQL table

CREATE TABLE [dbo].[Tbl_Data](
       [Id] [int] IDENTITY(1,1) NOT NULL,
       [UserName] [varchar](50) NULL,
       [EmailId] [varchar](50) NULL,
       [Password] [varchar](50) NULL,
PRIMARY KEY CLUSTERED
(
       [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

------------

ADO.NET C# code to insert data into SQL database table and to view the data
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace mani
{
    public partial class home : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Btn_Insert_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-R72EIR5\SQLEXPRESS;Initial Catalog=mk;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("insert Tbl_Data(Username,EmailId,Password) Values('" + Txt_UserName.Text + "','" + Txt_EmailId.Text + "','" + Txt_Password.Text + "')", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Txt_UserName.Text = "";
            Txt_EmailId.Text = "";
            Txt_Password.Text = "";
            Response.Write("inserted Successfully");

        }

        protected void Btn_Select_Click1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-R72EIR5\SQLEXPRESS;Initial Catalog=mk;Integrated Security=True");
            SqlCommand cmd = new SqlCommand("Select * From Tbl_Data", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GV_Select.DataSource = ds;
            GV_Select.DataBind();

        }
    }
}

Posted by Sudhir Chekuri
Tag :

Followers

Total Pageviews

Powered by Blogger.

- Copyright © 2013 DevStudent - Metrominimalist - Powered by Blogger - Designed by Johanes Djogan -