Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace Final
{
public partial class Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["App"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
Label7.Visible = true;
if (IsPostBack == false)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
//string strn = "select * from Dtls";
//DataSet ds = new DataSet();
//SqlDataAdapter da = new SqlDataAdapter(strn, conn);
//da.Fill(ds,"tab");
//int i = ds.Tables["Tab"].Rows.Count;
//if (ds.Tables.Count > 0)
//{
// DataGrid1.DataSource = ds;
// DataGrid1.DataBind();
//}
string strn = "select * from Dtls";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strn, conn);
da.Fill(ds, "tab");
int i = ds.Tables["Tab"].Rows.Count;
if (ds.Tables.Count > 0)
{
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "Name";
// DropDownList1.DataValueField = "id";
DropDownList1.DataBind();
}
}
}
void clear()
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string str = "insert into dtls values('" + TextBox1.Text + "','" + TextBox2.Text + "',convert(smalldatetime,'" + TextBox3.Text + "',103),'" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')";
SqlCommand cmd = new SqlCommand(str, conn);
cmd.ExecuteNonQuery();
conn.Close();
clear();
Response.Redirect("Default.aspx");
}
protected void Button3_Click(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string str = "delete from dtls where Name= '"+DropDownList1.SelectedItem+ "'";
SqlCommand cmd = new SqlCommand(str, conn);
cmd.ExecuteNonQuery();
conn.Close();
Response.Redirect("Default.aspx");
}
protected void Button6_Click(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string str = "update Dtls set Name='" + TextBox1.Text + "', Address='" + TextBox2.Text + "' where Name='" + DropDownList1.SelectedValue + "'";
SqlCommand cmd = new SqlCommand(str, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == "SELECT")
{
Label lbl = e.Item.FindControl("Label10") as Label;
string sdt = lbl.Text;
ViewState["ag"] = sdt;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//if (conn.State == ConnectionState.Closed)
//{
// conn.Open();
//}
//string str = "Select Name,Address,DOB,Profession,Email,Mob from Dtls where Name='" + DropDownList1.SelectedValue + "' ";
//DataSet ds = new DataSet();
//SqlDataAdapter da = new SqlDataAdapter(str, conn);
//da.Fill(ds, "f1");
//if (ds.Tables["f1"].Rows.Count > 0)
//{
// TextBox1.Text = ds.Tables["f1"].Rows[0].ItemArray[0].ToString();
// //incase kittiyillel ithu kodukkanam
// TextBox1.Text = ds.Tables["f1"].Rows[0].ItemArray.Contains("Name").ToString();
// TextBox2.Text = ds.Tables["f1"].Rows[0].ItemArray[1].ToString();
// TextBox3.Text = ds.Tables["f1"].Rows[0].ItemArray[2].ToString();
// TextBox4.Text = ds.Tables["f1"].Rows[0].ItemArray[3].ToString();
// TextBox5.Text = ds.Tables["f1"].Rows[0].ItemArray[3].ToString();
// TextBox6.Text = ds.Tables["f1"].Rows[0].ItemArray[3].ToString();
}
protected void TextBox7_TextChanged(object sender, EventArgs e)
{
Label7.Visible = false;
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Dtls where Name like '%" + TextBox7.Text + "%'", conn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
ListBox1.Items.Add(dr["Name"].ToString());
ListBox1.Visible = true;
}
dr.Close();
conn.Close();
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox7.Text = ListBox1.SelectedItem.Text;
ListBox1.Visible = false;
ListBox1.Items.Clear();
}
protected void Button5_Click(object sender, EventArgs e)
{
Label7.Text = TextBox7.Text;
ListBox1.Visible = true;
}
}
}
Comment