Saturday 4 August 2012

select data from one database and insert into another table


select data from one database and insert into another table

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
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd=new SqlCommand();
    SqlDataAdapter da=new SqlDataAdapter();
   DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Show();
        }
        BtnAdd.Attributes.Add("OnClick","javascript:return validate();");
    }
    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            cmd = new SqlCommand("insert into STUDENT1 values('" + txtStuName.Text + "','" + txtFatName.Text + "','" + txtVillName.Text +
 "','" + DDLcourse.SelectedItem.Text + "','" + DDLyear.SelectedItem.Text + "')", con);
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();
        }
        catch (Exception ex)
        {
            Response.Write("These values are already selected");
            //txtStuName.Text = "";
            //txtFatName.Text="";
            //txtVillName.Text="";
            //DDLcourse.SelectedIndex = 0;
            //DDLyear.SelectedIndex = 0;
        }
        finally
        {
            con.Close();
        }
    }
    protected void BtnReset_Click(object sender, EventArgs e)
    {
        txtStuName.Text = "";
        txtFatName.Text = "";
        txtVillName.Text = "";
       
    }
    protected void BtnDelete_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("delete from STUDENT1 where StuName='"+txtStuName.Text+"'",con);
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
       
        {
          


                cmd = new SqlCommand("update STUDENT1 set Course='" + DDLcourse.SelectedItem.Text + "' where StuName='" + txtStuName.Text + "'", con);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
          
        }
     
   
   public void Show()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
          cmd = new SqlCommand("select * from STUDENT1",con);
            dt = new DataTable();
            da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
                Label1.Text = "No Rows in a Table";
            }
       
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        Show();
    }



    protected void Cancel(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        Show();
    }
}
------Default.aspx-source.code------
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Student Information</title>
    <script type="text/javascript">
    function validate()
    {
      if(document.getElementById("txtStuName").value=="")
      {
        alert("Please enter the student name");
        document.getElementById("txtStuName").focus();
        return false;
      }
       if(document.getElementById("txtFatName").value=="")
       {
          alert("Please enter the Strdent father name");
          document.getElementById("txtFatName").focus();
          return false;
       }
       if(document.getElementById("txtVillName").value=="")
       {
       alert("Please enter the village name");
       document.getElementById("txtVillName").focus();
       return false;
       }
       if(document.getElementById("DDLcourse").selectedIndex==0)
       {
       alert("Please select course");
       document.getElementById("DDLcourse").focus();
       return false;
       }
       if(document.getElementById("DDLyear").selectedIndex==0)
       {
       alert("Please select year");
       document.getElementById("DDLyear").focus();
       return false;
       }
       else
       {
       return true;
       }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center">
    <tr><td>Enter the Student Name</td><td>
        <asp:TextBox ID="txtStuName" runat="server"></asp:TextBox></td></tr>
    <tr><td>Enter the Father Name</td><td>
        <asp:TextBox ID="txtFatName" runat="server"></asp:TextBox></td></tr>
    <tr><td>Enter the village Name</td><td>
        <asp:TextBox ID="txtVillName" runat="server"></asp:TextBox></td></tr>
    <tr><td>Course</td><td>
        <asp:DropDownList ID="DDLcourse" runat="server">
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem Value="M.C.A"></asp:ListItem>
            <asp:ListItem>B.tech</asp:ListItem>
            <asp:ListItem Value="Mtech"></asp:ListItem>
            <asp:ListItem Value="Bphar"></asp:ListItem>
            <asp:ListItem>Mphar</asp:ListItem>
        </asp:DropDownList></td></tr>
    <tr><td>Year</td><td>
        <asp:DropDownList ID="DDLyear" runat="server">
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem>1995</asp:ListItem>
            <asp:ListItem>1996</asp:ListItem>
            <asp:ListItem>1997</asp:ListItem>
            <asp:ListItem>1998</asp:ListItem>
            <asp:ListItem>1999</asp:ListItem>
            <asp:ListItem Value="2000"></asp:ListItem>
            <asp:ListItem>2001</asp:ListItem>
            <asp:ListItem>2002</asp:ListItem>
            <asp:ListItem Value="2003">2003</asp:ListItem>
        </asp:DropDownList></td></tr>
        <tr><td colspan="2" align="center">
            <asp:Button ID="BtnAdd" runat="server" Text="Add" OnClick="BtnAdd_Click" />&nbsp;
            <asp:Button ID="BtnReset" runat="server" Text="Reset" OnClick="BtnReset_Click" />&nbsp;
            <asp:Button ID="BtnDelete" runat="server" Text="Delete" OnClick="BtnDelete_Click" />&nbsp;
            <asp:Button ID="BtnUpdate" runat="server" Text="Update" OnClick="BtnUpdate_Click" /></td></tr>
    </table>
   
    </div>
        <asp:Label ID="Label1" runat="server" Style="position: relative" Text="Label"></asp:Label>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"
            Style="left: 231px; position: relative; top: -3px" AutoGenerateEditButton="True" OnRowCancelingEdit="Cancel" OnRowEditing="GridView1_RowEditing">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
    </form>
</body>
</html>

 

No comments:

Post a Comment

Popular posts