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).ToShort
DateString
();
}
#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.CancelComma
nd += new System.Web.UI.WebControls.
DataGridCo
mmandEvent
Handler(th
is.DataGri
d1_Cancel)
;
this.DataGrid1.EditCommand
+= new System.Web.UI.WebControls.
DataGridCo
mmandEvent
Handler(th
is.DataGri
d1_Edit);
this.DataGrid1.UpdateComma
nd += new System.Web.UI.WebControls.
DataGridCo
mmandEvent
Handler(th
is.DataGri
d1_Update)
;
this.Load += new System.EventHandler(this.P
age_Load);
}
#endregion
public void GetRecs_Click(object sender, System.EventArgs e)
{
BindData();
}
private void DataGrid1_Edit(object source, System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
}
private void DataGrid1_Cancel(object source, System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args e)
{
DataGrid1.EditItemIndex = -1;
BindData();
}
private void DataGrid1_Select(object source, System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args e)
{
Label2.Text +=DataGrid1.SelectedItem.C
ells[0].Te
xt;
//BindData();
}
private void DataGrid1_Update(object source, System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args e)
{
string StrConnection;
System.Web.UI.WebControls.
TextBox cName = new System.Web.UI.WebControls.
TextBox();
cName = (System.Web.UI.WebControls
.TextBox)e
.Item.Cell
s[6].Contr
ols[0];
StrConnection = "Password=xxx;Persist Security Info=True;User ID=sa;Initial Catalog=Duc;Data Source=SQL001";
SqlConnection myConnection = new SqlConnection(StrConnectio
n);
SqlCommand myCommand = new SqlCommand("SP_UpdateQty",
myConnection);
myCommand.CommandType = CommandType.StoredProcedur
e;
// myCommand.Parameters.Add(n
ew SqlParameter("@product", SqlDbType.VarChar,20));
// myCommand.Parameters["@pro
duct"].Val
ue = cName.Text;
// myCommand.Parameters.Add(n
ew SqlParameter("@harvest_dat
e", SqlDbType.DateTime,8));
// myCommand.Parameters["@har
vest_date"
].Value = cName.Text;
// myCommand.Parameters.Add(n
ew SqlParameter("@prod_size",
SqlDbType.Int,4));
// myCommand.Parameters["@pro
d_size"].V
alue = cName.Text;
myCommand.Parameters.Add(n
ew SqlParameter("@quantity", SqlDbType.Int,4));
myCommand.Parameters["@qua
ntity"].Va
lue = 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.Pa
rameters.A
dd(new SqlParameter("@product", SqlDbType.VarChar, 20));
myCommand.SelectCommand.Pa
rameters["
@product"]
.Value = MySelect.Value;
myCommand.SelectCommand.Pa
rameters.A
dd(new SqlParameter("@FText", SqlDbType.SmallDateTime, 8));
myCommand.SelectCommand.Pa
rameters["
@FText"].V
alue = FText.Value;
myCommand.SelectCommand.Pa
rameters.A
dd(new SqlParameter("@TText", SqlDbType.SmallDateTime, 8));
myCommand.SelectCommand.Pa
rameters["
@TText"].V
alue = TText.Value;
DataSet ds = new DataSet();
myCommand.Fill(ds, "Harvest1");
DataGrid1.DataSource = ds.Tables["Harvest1"].Defa
ultView;
DataGrid1.DataBind();
}