Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 29th, 2005, 02:31 PM
Registered User
 
Join Date: Nov 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default DataGrid_Update

Hi,
I have this code for my DataGrid_Update where in I am trying to update a field, which is kind of acting up, as it is urgent if someone can take a lokk at it i will really appreciate it.

Thanks,

Here is the whole code.

     private void Page_Load(object sender, System.EventArgs e)
          {
               DateTime DtDate = new DateTime();
               DtDate = DateTime.Now;
               FText.Value = DtDate.ToShortDateString();
               TText.Value = DtDate.AddDays(13).ToShortDateString();
          }

          #region Web Form Designer generated code
          override protected void OnInit(EventArgs e)
          {
               //
               // CODEGEN: This call is required by the ASP.NET Web Form Designer.
               //
               InitializeComponent();
               base.OnInit(e);
          }

          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
               this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_Cancel);
               this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_Edit);
               this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_Update);
               this.Load += new System.EventHandler(this.Page_Load);

          }
          #endregion

          public void GetRecs_Click(object sender, System.EventArgs e)
          {
               BindData();
          }


          private void DataGrid1_Edit(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
          {
               DataGrid1.EditItemIndex = e.Item.ItemIndex;
               BindData();
          }

          private void DataGrid1_Cancel(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
          {
               DataGrid1.EditItemIndex = -1;
               BindData();
          }

          private void DataGrid1_Select(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
          {
               Label2.Text +=DataGrid1.SelectedItem.Cells[0].Text;
               //BindData();
          }

          private void DataGrid1_Update(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
          {
               string StrConnection;
               System.Web.UI.WebControls.TextBox cName = new System.Web.UI.WebControls.TextBox();
               cName = (System.Web.UI.WebControls.TextBox)e.Item.Cells[6].Controls[0];
StrConnection = "Password=xxx;Persist Security Info=True;User ID=sa;Initial Catalog=Duc;Data Source=SQL001";
               SqlConnection myConnection = new SqlConnection(StrConnection);
               SqlCommand myCommand = new SqlCommand("SP_UpdateQty", myConnection);
               myCommand.CommandType = CommandType.StoredProcedure;
// myCommand.Parameters.Add(new SqlParameter("@product", SqlDbType.VarChar,20));
// myCommand.Parameters["@product"].Value = cName.Text;
// myCommand.Parameters.Add(new SqlParameter("@harvest_date", SqlDbType.DateTime,8));
// myCommand.Parameters["@harvest_date"].Value = cName.Text;
// myCommand.Parameters.Add(new SqlParameter("@prod_size", SqlDbType.Int,4));
// myCommand.Parameters["@prod_size"].Value = cName.Text;
            myCommand.Parameters.Add(new SqlParameter("@quantity", SqlDbType.Int,4));
               myCommand.Parameters["@quantity"].Value = cName.Text;
               myConnection.Open();
               myCommand.ExecuteNonQuery();
               myConnection.Close();
               DataGrid1.EditItemIndex = -1;
               BindData();
          }

          public void BindData()
          {
               string StrConnection;
string selectCmd = "select * from harvest1 where quantity > 0 and product = @product and harvest_Date Between @FText and @TText";

StrConnection = "Password=xxx;Persist Security Info=True;User ID=sa;Initial Catalog=Duc;Data Source=SQL001";
               SqlDataAdapter myCommand = new SqlDataAdapter(selectCmd, StrConnection);

     myCommand.SelectCommand.Parameters.Add(new SqlParameter("@product", SqlDbType.VarChar, 20));
               myCommand.SelectCommand.Parameters["@product"].Value = MySelect.Value;

     myCommand.SelectCommand.Parameters.Add(new SqlParameter("@FText", SqlDbType.SmallDateTime, 8));
               myCommand.SelectCommand.Parameters["@FText"].Value = FText.Value;

     myCommand.SelectCommand.Parameters.Add(new SqlParameter("@TText", SqlDbType.SmallDateTime, 8));
               myCommand.SelectCommand.Parameters["@TText"].Value = TText.Value;

               DataSet ds = new DataSet();
               myCommand.Fill(ds, "Harvest1");

               DataGrid1.DataSource = ds.Tables["Harvest1"].DefaultView;
               DataGrid1.DataBind();

          }

 
Old November 29th, 2005, 03:50 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If it's urgent, you better post more information. "acting up" is hardly a problem description that people on this forum can use to start diagnosing your situation...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.