Saturday 8 June 2013

Insert Country

Insert Country:
.ASPX CODE:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Country Insert.aspx.cs" Inherits="Country_Insert" %>

<!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>Insert Country</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div>
    Country Name:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click"
            Text="Insert Country" />
   
    </div>
    <asp:DropDownList ID="DropDownList1" runat="server" >
    </asp:DropDownList>
    </div>
    </form>
</body>
</html>

.CS CODE:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Country_Insert : System.Web.UI.Page
{
    string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
      

    }
    protected void btn_insert_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(str);
        con.Open();

        SqlCommand com = new SqlCommand("insert into country values(@countryName)", con);
        com.Parameters.AddWithValue("@countryName", TextBox1.Text);
        com.ExecuteNonQuery();
      
        con.Close();
        binddropdownlist();
      

    }

    public void binddropdownlist()
    {
        SqlConnection cn = new SqlConnection(str);
        SqlCommand cmd = new SqlCommand("select * from country", cn);
        SqlDataAdapter adpt = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adpt.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "countryName";
        DropDownList1.DataValueField = "countryID";
        DropDownList1.DataBind();

    }

   


  


}


No comments:

Post a Comment