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();
}
|