0

I tried to update the data in edit mode of gridview.

It does not send correctly with my actual code. I tried to make it a templateField in HTML and it does not work. At this moment,the program break when I modify the date in the edit mode of gridview. This is put in Page_Load.In the condition to sort the grid, the grid is binding.

     if (ViewState["sorting"] == null)
    {

        String myquery = "Select * from Venituri";
        SqlConnection sqlCon = new SqlConnection(CS);
        SqlCommand cmd = new SqlCommand
        {
            CommandText = myquery,
            Connection = sqlCon
        };
        SqlDataAdapter da = new SqlDataAdapter
        {
            SelectCommand = cmd
        };
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridViewIncomes.DataSource = ds;
        GridViewIncomes.DataSourceID = String.Empty;
        GridViewIncomes.DataBind(); //here is a break when I was modified with the suggest code
    }




protected void GridViewIncomes_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    SqlConnection sqlCon = new SqlConnection(CS);
    int index = GridViewIncomes.EditIndex;
    GridViewRow row = GridViewIncomes.Rows[index];
    int VenitId = Convert.ToInt32(GridViewIncomes.DataKeys[e.RowIndex].Value);
    string Denumire = ((TextBox)row.Cells[2].Controls[0]).Text.ToString().Trim();
    var MyDateInsCalendar = GridViewIncomes.Rows[GridViewIncomes.EditIndex].FindControl("Data") as Calendar;
    MyDateInsCalendar.Visible = false;
    string Suma = ((TextBox)row.Cells[4].Controls[0]).Text.ToString().Trim();
    string Descriere = ((TextBox)row.Cells[5].Controls[0]).Text.ToString().Trim();
    string sql = "UPDATE Venituri SET Denumire='" + Denumire + "',Data='" + MyDateInsCalendar + "',Suma='" + Suma + "',Descriere='" + Descriere + "' WHERE VenitId=" + VenitId + "";

    SqlCommand cmd = new SqlCommand(sql, sqlCon);
    sqlCon.Open();
    int temp = cmd.ExecuteNonQuery();
    sqlCon.Close();
    if (temp == 1)
    {

        lblSuccessMessage.Text = "Actualizat cu succes!";
    }
    GridViewIncomes.EditIndex = -1;
    lblSuccessMessage.Text = "";

}

`

.aspx <asp:BoundField HeaderText="Data" SortExpression="Data" DataField="Data" />

To edit the date into gridview and update in the database.

piglet
  • 7
  • 8

1 Answers1

0

Your MyDateInsCalendar variable is an objcect (Calendar). Use one of the properties of this variable in your SQL statement.

for example:

<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="true" OnRowEditing="GridView1_RowEditing" AutoGenerateColumns="false" OnRowUpdating="GridView1_RowUpdating">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>                
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid();
    }
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindGrid();
}

private void BindGrid()
{
    List<string> tmp = new List<string>();
    tmp.Add("a");
    GridView1.DataSource = tmp;
    GridView1.DataBind();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    Calendar cal = GridView1.Rows[e.RowIndex].FindControl("Calendar1") as Calendar;
    string tmp = cal.SelectedDate.ToString();
    BindGrid();
}
Zsmaster
  • 1,549
  • 4
  • 19
  • 28
  • It's not working. The error is: reference to an null object. I think the problem is at this line: var MyDateInsCalendar = GridViewIncomes.Rows[GridViewIncomes.EditIndex].FindControl("Data") as Calendar; – piglet May 21 '19 at 13:56
  • I hope this example will be help for you – Zsmaster May 21 '19 at 16:23
  • I edit my code above. I can not use totally your suggestion because I had a SQLData Source and can not bind twice. I use only the part with calendar. I got the following error: The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.' – piglet May 22 '19 at 05:22
  • Your MyDateInsCalendar variable is an object (Calendar). Use one of the properties of this variable in your SQL statement. for example: MyDateInsCalendar.SelectedDate.To.String() – Zsmaster May 22 '19 at 07:04
  • From your code I copy the lines in Grid_Updating; I use in SQL statement 'tmp', is this the same thing,right? – piglet May 22 '19 at 08:19
  • I found a solutin within the code does not break. 'DateTime del = DateTime.ParseExact(((TextBox)GridViewIncomes.Rows[e.RowIndex].FindControl("tbDataEdit")).Text,"dd/mm/yyyy",null);' but throws an exception: 'String was not recognized as a valid DateTime.' del={1 Jan 0001 00:00:00}. – piglet May 22 '19 at 18:07