ASP.NET - Textbox1.text change event

Asked By bhanupratap singh
10-Feb-12 12:16 AM
I want to search data from table using for loop with dataset not datareader. On textchange event
how ?..


 
  Somesh Yadav replied to bhanupratap singh
10-Feb-12 12:22 AM
Hi,

here is the code .

GridColumnStylesCollection gcsColl =
  dgConversionTable.TableStyles[“Demo”].GridColumnStyles;
for (int i=0; i< gcsColl.Count; i++)
{
  if(gcsColl[i].GetType() == typeof(DataGridTextBoxColumn))
  {
   DataGridTextBoxColumn textColumn = (DataGridTextBoxColumn)gcsColl[i];
   If (textColumn.HeaderText == “old header text”)
   {
     textColumn.Width = 150;
     textColumn.HeaderText = “new header text”;
     break;
   }
  }
}
  Venkat K replied to bhanupratap singh
10-Feb-12 12:39 AM
it seems you are looking to loop datatable exist under dataset:

foreach (DataRow myRow in DataSet.Tables["yourtablename"].Rows)
          {
            // here you can access myRow object
            //Ex.
            myRow["columnname"] = "your value";
          }

Thanks
  bhanupratap singh replied to Somesh Yadav
10-Feb-12 12:43 AM
This not a gridview I put value into textbox then i want to check this value into table wether it exist or not. If exist then other column of this row will be displayed into another textbox. Note one thing:- I have to find the value using dataset
  bhanupratap singh replied to Venkat K
10-Feb-12 01:04 AM
Yes Frind you got me.
Below is my code see where I commit mistake.
Pls help me i m big trouble.
Thanks

 protected void txtcnmt_TextChanged(object sender, EventArgs e)
        {
            
            SqlCommand cmdchekcnmt = new SqlCommand(" Select *from Consignment", con);
            con.Open();
            cmdchekcnmt.CommandType = CommandType.Text;
            DataSet ds = new DataSet();
            SqlDataAdapter dap = new SqlDataAdapter(cmdchekcnmt);
            dap.Fill(ds, "Cons");
         
            foreach (DataRow myRow in ds.Tables["Cons"].Rows)
            {
                if (myRow["CNMT"] == txtcnmt.Text)
                {
                    Response.Write(myRow);
// but curssor does not come in-- cnmt is column name in my table want to compaire with textbox value .
// If it matches the other column of this row will be display in other textboxes. Thanks
                }
                else
                {
                    Response.Write("Nothing");
                }
            }
         }
  Sreekumar P replied to bhanupratap singh
10-Feb-12 02:04 AM
Hi,

try this code

protected void txtcnmt_TextChanged(object sender, EventArgs e)
  {
 
    SqlCommand cmdchekcnmt = new SqlCommand(" Select * from Consignment", con);
    con.Open();
    cmdchekcnmt.CommandType = CommandType.Text;
    DataSet ds = new DataSet();
    SqlDataAdapter dap = new SqlDataAdapter(cmdchekcnmt);
    dap.Fill(ds, "Cons");
 
    string value = "";
    foreach (DataRow myRow in ds.Tables["Cons"].Rows)
    {
      if (myRow["CNMT"].ToString().Contains(txtcnmt.Text))
      {
        value = myRow["CNMT"].ToString();
        break;
      }
    }
    if (value == "")
      Response.Write("Nothing");
    else
      txtcnmt.Text = value;
  }
  bhanupratap singh replied to Sreekumar P
10-Feb-12 05:09 AM
but how to use this,  using for loop not Datarow. Like :----
i=0;i<ds.tables["Cons"].row[0]["CNMT"];i++
How to implement it
  Sreekumar P replied to bhanupratap singh
10-Feb-12 05:16 AM
Hi,

I got ur Ques... that u want to replace the foreach loop with for loop.

here is the code, but i can say that the above code is more optimized (faster)

protected void txtcnmt_TextChanged(object sender, EventArgs e)
     {
 
       SqlCommand cmdchekcnmt = new SqlCommand(" Select * from Consignment", con);
       con.Open();
       cmdchekcnmt.CommandType = CommandType.Text;
       DataSet ds = new DataSet();
       SqlDataAdapter dap = new SqlDataAdapter(cmdchekcnmt);
       dap.Fill(ds, "Cons");
 
       string value = "";
       for(int i=0;i<=ds.Tables["Cons"].Rows.Count;i++)
       {
         if (ds.Tables["Cons"].Rows[i]["CNMT"].ToString().Contains(txtcnmt.Text))
         {
           value = ds.Tables["Cons"].Rows[i]["CNMT"].ToString();
           break;
         }
       }
       if (value == "")
         Response.Write("Nothing");
       else
         txtcnmt.Text = value;
     }
  dipa ahuja replied to bhanupratap singh
10-Feb-12 06:13 AM
You need to use the ajaxUpdate Panel for this

Markup
 
<asp:UpdatePanel runat="server" ID="up1">
  <ContentTemplate>
    Username:
    <asp:TextBox runat="server" ID="Username" AutoPostBack="true" OnTextChanged="Username_Changed" />
    <div runat="server" id="UserAvailability">
    </div>
    <br />
  </ContentTemplate>
</asp:UpdatePanel>
 
Code Behind:
 
protected void Username_Changed(object sender, EventArgs e)
{
  if (!checkExist(Username.Text))
  {
    UserAvailability.InnerText = "Username taken, sorry.";     
  }
  else
  {
    UserAvailability.InnerText = "Username available!";
  }
}
 
public bool checkExist(string s)
{
  string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
   
  SqlDataAdapter da = new SqlDataAdapter("Select name from people where name='"
    + s + "'", conn);
 
  DataTable dt = new DataTable();
  da.Fill(dt);
 
  if (dt.Rows.Count > 0)
  {
    return false;
  }
  else
  {
    return true;
  }
}
 

  bhanupratap singh replied to Sreekumar P
10-Feb-12 10:45 AM
Thanks for reply.
I will be touch in u very soon.
thanks again
  Sreekumar P replied to bhanupratap singh
10-Feb-12 01:17 PM
always welcome ... :)
  bhanupratap singh replied to Sreekumar P
11-Feb-12 01:13 AM
I need your help look on below code. Why it create new blank row in gridview when clik update button
I need your email Id personaly to contact u. My email id is bpsingh@ymail.com
Thanks
-----------------------
 private void BindGrid(int rowcount)
        {
            DataTable dt = new DataTable();
            DataRow dr;

            dt.Columns.Add(new System.Data.DataColumn("txtcode", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("txtcnmt", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("txtpkg", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("txtweight", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("ddlgrvtoo", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("txtfreight", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("txtdescription", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("txtmarka", typeof(String)));
            dt.Columns.Add(new System.Data.DataColumn("txt12", typeof(String)));


            if (ViewState["CurrentData"] != null)
            {
                for (int i = 0; i < rowcount + 1; i++)
                {
                    dt = (DataTable)ViewState["CurrentData"];
                    if (dt.Rows.Count > 0)
                    {
                        dr = dt.NewRow();
                        dr[0] = dt.Rows[0][0].ToString();


                    }
                }


                dr = dt.NewRow();


                dr[0] = txtcode.Text;
                dr[1] = txtcnmt.Text;
                dr[2] = txtpkg.Text;
                dr[3] = txtweight.Text;
               // dr[4] = ddlgrvtoo.SelectedItem.Text;
                dr[4] = ddlgrvtoo.Text;
                dr[5] = txtfreight.Text;
                dr[6] = txtdescription.Text;
                dr[7] = txtmarka.Text;
                dr[8] = txt12.Text;


                dt.Rows.Add(dr);
            }
            else
            {
                dr = dt.NewRow();
                dr[0] = txtcode.Text;
                dr[1] = txtcnmt.Text;
                dr[2] = txtpkg.Text;
                dr[3] = txtweight.Text;
                //dr[4] = ddlgrvtoo.SelectedItem.Text;
                dr[4] = ddlgrvtoo.Text;
                dr[5] = txtfreight.Text;
                dr[6] = txtdescription.Text;
                dr[7] = txtmarka.Text;
                dr[8] = txt12.Text;


                dt.Rows.Add(dr);


            }


            // If ViewState has a data then use the value as the DataSource


            if (ViewState["CurrentData"] != null)
            {

                GrvMfDetails.DataSource = (DataTable)ViewState["CurrentData"];
                GrvMfDetails.DataBind();
            }
            else
            {
                // Bind GridView with the initial data assocaited in the DataTable
                GrvMfDetails.DataSource = dt;
                GrvMfDetails.DataBind();
            }
            // Store the DataTable in ViewState to retain the values
            ViewState["CurrentData"] = dt;
       
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            if (txtcode.Text == "")
            {
                Response.Write("Pls Enter Code No");
                txtcode.Focus();
                return;
            }
            // Check if the ViewState has a data assoiciated within it. If
            if (ViewState["CurrentData"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentData"];
                int count = dt.Rows.Count;
                BindGrid(count);
            }
            else
            {
                BindGrid(1);
            }
            txtcode.Text = "";
            txtcnmt.Text = "";
            txtpkg.Text = "";
            txtweight.Text = "";
            ddlgrvtoo.Items.Clear();
            txtdescription.Text = "";
            txtfreight.Text = "";
            txtmarka.Text = "";


        }


        
        protected void txtcnmt_TextChanged(object sender, EventArgs e)
        {
            
            SqlCommand cmdchekcnmt = new SqlCommand(" Select *from Consignment", con);
            con.Open();
            cmdchekcnmt.CommandType = CommandType.Text;
            DataSet ds = new DataSet();
            SqlDataAdapter dap = new SqlDataAdapter(cmdchekcnmt);
            dap.Fill(ds, "Cons");
            foreach (DataRow myRow in ds.Tables["Cons"].Rows)
            {                
                string cnmt = myRow["CNMT"].ToString();
                if (cnmt == txtcnmt.Text)
                {
                    txtpkg.Text = myRow["NOPKG"].ToString();
                    txtweight.Text = myRow["AWGHT"].ToString();
                    ddlgrvtoo.Items.Clear();
                    string stn = myRow["TOO"].ToString();
                    ddlgrvtoo.Items.Add(stn);
                    txtfreight.Text = myRow["FREIGHT"].ToString();
                    txtdescription.Text = myRow["D_GOOD"].ToString();
                    //txtmarka.Text = myRow[""].ToString();
                                       
                }
            }
            txtpkg.Focus();
         }


        protected void GrvMfDetails_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GrvMfDetails.EditIndex = e.NewEditIndex;
            BindGrid(1);
        }


        protected void GrvMfDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
              

            DataTable dt = (DataTable)ViewState["CurrentData"];
            GridViewRow row = GrvMfDetails.Rows[e.RowIndex];
            dt.Rows[row.DataItemIndex]["txtcode"] = ((TextBox)(row.Cells[0].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["txtcnmt"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["txtpkg"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["txtweight"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["ddlgrvtoo"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["txtfreight"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["txtdescription"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["txtmarka"] = ((TextBox)(row.Cells[7].Controls[0])).Text;
            dt.Rows[row.DataItemIndex]["txt12"] = ((TextBox)(row.Cells[8].Controls[0])).Text;
            GrvMfDetails.EditIndex = -1;
            GrvMfDetails.DataSource = dt;
            GrvMfDetails.DataBind();
        }


        protected void GrvMfDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GrvMfDetails.EditIndex = -1;
            BindGrid(1);
        }
Create New Account
help
SqlConnection = new SqlConnection([Your Connections String]) dim ds as new DataSet dim daTable1 as new SqlDataAdapter dim daTable2 as new SqlDataAdapter dim daTable3 as new SqlDataAdapter dim daTable4 as new SqlDataAdapter daTable1.SelectCommand = "select * from table1 where . . ." daTable2.SelectCommand = "select * from table2 where . . ." daTable3.SelectCommand = "select attempting to recreate what you mentioned however, I cannot find any means of producing a SqlDataAdapter in VS2005. I appear to only have the option to create SqlDataSources. What hapened to post_date, author, comments, ct_number FROM Comments WHERE ct_number = ctpt.ct_number"; SqlConnection conn = new SqlConnection(strConn); SqlDataAdapter da = new SqlDataAdapter(strSql, conn); da.TableMappings.Add("Customers1", "Customers"); da.TableMappings.Add("Customers2", "CTPT"); da.TableMappings.Add post_date, author, comments, ct_number FROM Comments WHERE ct_number = ctpt.ct_number"; SqlConnection conn = new SqlConnection(conn); SqlDataAdapter da = new SqlDataAdapter(strSql, conn); da.TableMappings.Add("Customers1", "Customers"); da.TableMappings.Add("Customers2", "CTPT
SqlConnection("Data Source = 10.0.2.8;Initial Catalog = JitendraDB;User ID = sa;password = change_123"); SqlDataAdapter da; string mySQL = "SELECT empid, empname, dept FROM emp "; da = new SqlDataAdapter(mySQL, con); con.Open(); DataSet ds = new DataSet(); da.Fill(ds); GridView2.DataSource = ds; GridView2 Write("<script> alert('Record Deleted')< / script> "); getdata(); } Code to edit protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e) { GridView2.EditIndex = e.NewEditIndex; getdata(); } protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView2.EditIndex = -1; getdata(); } Code to Update protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e) { string strEmpId = GridView1.Rows[e.RowIndex].Cells[1].Text; / / FOR GETTING ENPID string strEmpName inside the code. String fn; String path; SqlConnection cnn = new SqlConnection(); SqlCommand cmd = new SqlCommand(); SqlDataAdapter adp; DataTable dt; Int32 id; / / id as a integer variable it will be used to am using the sql query to select the gecord from the database and adp = new SqlDataAdapter( "SELECT * FROM tb_gallery " , cnn); / / here i declare the datatable to fill the record dt = new sql query to select the record that will be selected by the user for deletion SqlDataAdapter adp = new SqlDataAdapter( "select * from tb_gallery where id = @id" , cnn); / / here i am passing the
to get the problems. Is there any good way to update multiple tables by using SqlDataAdapter.Update()? Thank you / * New Rows in ParentTable and ChildTable * * - -- -- -ParentTable- -- -- - * ID Animal * 1 Cat * * * - -- -- -ChildTable first way I use is to follow the update sequence recommended by book: adapter1 = new SqlDataAdapter("Select * from ChildTable", con); SqlDataAdapter adapter2 = new SqlDataAdapter("Select * from ParentTable", con); SqlCommandBuilder sb = new SqlCommandBuilder(adapter1); SqlCommandBuilder sb1 = new SqlCommandBuilder(adapter2); adapter1 NewDataSet.Tables["ChildTable"].Select(null, null, DataViewRowState.Added)); - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- The 2nd way is to use 2 SqlDataAdapter seperately to update tables, but still get the same problem : The Child table row "3 small cat 1" is wrong adapter1 = new SqlDataAdapter("Select * from ChildTable", con); SqlDataAdapter adapter2 = new SqlDataAdapter("Select * from ParentTable", con); SqlCommandBuilder sb = new SqlCommandBuilder(adapter1); SqlCommandBuilder sb1 = new SqlCommandBuilder(adapter2); adapter1
about populating listboxes. very good. Can u tell me how I can clean the below? SqlDataAdapter title; DataSet ds1; title = new SqlDataAdapter ("Select * FROM titles ", m_strConnection); ds1 = new DataSet(); title.Fill (ds1, "Table"); lbTitle.DataSource = ds1.Tables Columns["title_name"].ColumnName.ToString(); lbTitle.DataValueField = ds1.Tables[0].Columns["title_id"].ColumnName.ToString(); lbTitle.DataBind () ; SqlDataAdapter gender; DataSet ds2; gender = new SqlDataAdapter ("Select * FROM genders ", m_strConnection); ds2 = new DataSet(); gender.Fill (ds2, "Table"); lbGender.DataSource = ds2.Tables Columns["gender_name"].ColumnName.ToString(); lbGender.DataValueField = ds2.Tables[0].Columns["gender_id"].ColumnName.ToString(); lbGender.DataBind () ; SqlDataAdapter secretwordtype; DataSet ds3; secretwordtype = new SqlDataAdapter ("Select * FROM secretwordtypes ", m_strConnection); ds3 = new DataSet(); secretwordtype.Fill (ds3, "Table"); lbSecretWordType.DataSource = ds3.Tables Columns["secretwordtype_name"].ColumnName.ToString(); lbSecretWordType.DataValueField = ds3.Tables[0].Columns["secretwordtype_id"].ColumnName.ToString(); lbSecretWordType.DataBind (); SqlDataAdapter state; DataSet ds4; state = new SqlDataAdapter ("Select * FROM states ", m_strConnection); ds4 = new DataSet(); state.Fill (ds4, "Table"); lbStateProvince.DataSource = ds4.Tables
EventArgs e) { if (!IsPostBack) { SqlConnection cn = new SqlConnection("con string"); string strQuery = "Select * from EmpImage"; SqlDataAdapter da = new SqlDataAdapter(strQuery, cn); DataSet ds = new DataSet(); da.Fill(ds); DropDownList1.DataSource = ds; DropDownList1.DataTextField = "COLUMN_NAME of DropDownList protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("con string "); SqlDataAdapter adapSel; string mySQL = "SELECT * from tablename where id = '" + DropDownList1.SelectedValue.ToString() + "'; adapSel = new SqlDataAdapter(mySQL, conn); DataSet dsSel = new DataSet(); adapSel.Fill(dsSel); Gridview .DataSource = dsSel; Gridview .DataBind(); conn sqlQuery = "Select * from table1 where columnName like " %+dropdown1.Text+%"; SqlCommand cmd = new SqlCommand(sqlQuery, conn) SqlDataAdapter da = New SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); gridView1.Datasource = dt; gridView1.DataBind(); } } In the SqlConnection("Data Source = 10.0.2.8;Initial Catalog = JitendraDB;User ID = sa;password = change_123"); SqlDataAdapter da; string mySQL = "SELECT empid, empname, dept FROM emp "; da = new SqlDataAdapter(mySQL, con); con.Open(); DataSet ds = new DataSet(); da.Fill(ds); GridView2.DataSource = ds; GridView2