By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Data Grid

P: 2
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. using System.Configuration;
  9. using System.Data;
  10.  
  11. namespace Final
  12. {
  13.     public partial class Default : System.Web.UI.Page
  14.     {
  15.         SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["App"].ConnectionString);
  16.         protected void Page_Load(object sender, EventArgs e)
  17.         {
  18.             Label7.Visible = true;
  19.             if (IsPostBack == false)
  20.             {
  21.                 if (conn.State == ConnectionState.Closed)
  22.                 {
  23.                     conn.Open();
  24.  
  25.                 }
  26.                 //string strn = "select * from Dtls";
  27.                 //DataSet ds = new DataSet();
  28.  
  29.  
  30.  
  31.                 //SqlDataAdapter da = new SqlDataAdapter(strn, conn);
  32.                 //da.Fill(ds,"tab");
  33.                 //int i = ds.Tables["Tab"].Rows.Count;
  34.                 //if (ds.Tables.Count > 0)
  35.                 //{
  36.                 //    DataGrid1.DataSource = ds;
  37.                 //    DataGrid1.DataBind();
  38.  
  39.                 //}
  40.                 string strn = "select *  from Dtls";
  41.                 DataSet ds = new DataSet();
  42.  
  43.  
  44.  
  45.                 SqlDataAdapter da = new SqlDataAdapter(strn, conn);
  46.                 da.Fill(ds, "tab");
  47.                 int i = ds.Tables["Tab"].Rows.Count;
  48.                 if (ds.Tables.Count > 0)
  49.                 {
  50.                     DropDownList1.DataSource = ds;
  51.                     DropDownList1.DataTextField = "Name";
  52.                   //  DropDownList1.DataValueField = "id";
  53.                     DropDownList1.DataBind();
  54.  
  55.  
  56.  
  57.  
  58.                 }
  59.             }
  60.         }
  61.  
  62.         void clear()
  63.         {
  64.             TextBox1.Text = "";
  65.             TextBox2.Text = "";
  66.             TextBox3.Text = "";
  67.             TextBox4.Text = "";
  68.             TextBox5.Text = "";
  69.             TextBox6.Text = "";
  70.         }
  71.         protected void Button2_Click(object sender, EventArgs e)
  72.         {
  73.  
  74.             if (conn.State == ConnectionState.Closed)
  75.             {
  76.                 conn.Open();
  77.             }
  78.  
  79.              string str = "insert into  dtls values('" + TextBox1.Text + "','" + TextBox2.Text + "',convert(smalldatetime,'" + TextBox3.Text + "',103),'" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')";
  80.              SqlCommand cmd = new SqlCommand(str, conn);
  81.              cmd.ExecuteNonQuery();
  82.              conn.Close();
  83.              clear();
  84.              Response.Redirect("Default.aspx");
  85.  
  86.         }
  87.  
  88.         protected void Button3_Click(object sender, EventArgs e)
  89.         {
  90.             if (conn.State == ConnectionState.Closed)
  91.             {
  92.                 conn.Open();
  93.             }
  94.             string str = "delete from  dtls where Name= '"+DropDownList1.SelectedItem+ "'";
  95.             SqlCommand cmd = new SqlCommand(str, conn);
  96.             cmd.ExecuteNonQuery();
  97.             conn.Close();
  98.             Response.Redirect("Default.aspx");
  99.  
  100.  
  101.         }
  102.  
  103.         protected void Button6_Click(object sender, EventArgs e)
  104.         {
  105.  
  106.             if (conn.State == ConnectionState.Closed)
  107.             {
  108.                 conn.Open();
  109.             }
  110.             string str = "update Dtls set Name='" + TextBox1.Text + "', Address='" + TextBox2.Text + "'  where Name='" + DropDownList1.SelectedValue + "'";
  111.             SqlCommand cmd = new SqlCommand(str, conn);
  112.             cmd.ExecuteNonQuery();
  113.             conn.Close();
  114.  
  115.         }
  116.  
  117.         protected void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
  118.         {
  119.             if (e.CommandName == "SELECT")
  120.             {
  121.  
  122.                 Label lbl = e.Item.FindControl("Label10") as Label;
  123.                 string sdt = lbl.Text;
  124.                 ViewState["ag"] = sdt;
  125.  
  126.             }
  127.         }
  128.  
  129.         protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  130.         {
  131.             //if (conn.State == ConnectionState.Closed)
  132.             //{
  133.             //    conn.Open();
  134.             //}
  135.             //string str = "Select Name,Address,DOB,Profession,Email,Mob from Dtls where Name='" + DropDownList1.SelectedValue + "' ";
  136.             //DataSet ds = new DataSet();
  137.             //SqlDataAdapter da = new SqlDataAdapter(str, conn);
  138.             //da.Fill(ds, "f1");
  139.             //if (ds.Tables["f1"].Rows.Count > 0)
  140.             //{
  141.             //    TextBox1.Text = ds.Tables["f1"].Rows[0].ItemArray[0].ToString();
  142.             //    //incase kittiyillel ithu kodukkanam 
  143.             //    TextBox1.Text = ds.Tables["f1"].Rows[0].ItemArray.Contains("Name").ToString();
  144.             //    TextBox2.Text = ds.Tables["f1"].Rows[0].ItemArray[1].ToString();
  145.             //    TextBox3.Text = ds.Tables["f1"].Rows[0].ItemArray[2].ToString();
  146.             //    TextBox4.Text = ds.Tables["f1"].Rows[0].ItemArray[3].ToString();
  147.             //    TextBox5.Text = ds.Tables["f1"].Rows[0].ItemArray[3].ToString();
  148.             //    TextBox6.Text = ds.Tables["f1"].Rows[0].ItemArray[3].ToString();
  149.  
  150.             }
  151.  
  152.         protected void TextBox7_TextChanged(object sender, EventArgs e)
  153.         {
  154.             Label7.Visible = false;
  155.             conn.Open();
  156.             SqlCommand cmd = new SqlCommand("select * from Dtls where Name like '%" + TextBox7.Text + "%'", conn);
  157.             SqlDataReader dr; 
  158.             dr = cmd.ExecuteReader();
  159.             while (dr.Read())
  160.             {
  161.               ListBox1.Items.Add(dr["Name"].ToString());
  162.               ListBox1.Visible = true;
  163.             }
  164.             dr.Close();
  165.  
  166.             conn.Close();
  167.         }
  168.  
  169.         protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
  170.         {
  171.  
  172.             TextBox7.Text = ListBox1.SelectedItem.Text;
  173.  
  174.             ListBox1.Visible = false;
  175.  
  176.             ListBox1.Items.Clear();
  177.         }
  178.  
  179.         protected void Button5_Click(object sender, EventArgs e)
  180.         {
  181.  
  182.             Label7.Text = TextBox7.Text;
  183.  
  184.             ListBox1.Visible = true;
  185.         }
  186.  
  187.  
  188.  
  189.  
  190.  
  191.         }
  192.  
  193.       }
Mar 8 '12 #1
Share this Question
Share on Google+
1 Reply


PsychoCoder
Expert Mod 100+
P: 465
Ok, now you've posted a butt load of code, so what's the issue you're having? You cant just post a bunch of code and think we're going to know what the issue is.
Mar 8 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.