ASP.NET - Textbox1.text change event - Asked By bhanupratap singh on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 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 on 10-Feb-12 01:17 PM
always welcome ... :)
bhanupratap singh replied to Sreekumar P on 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);
        }