Trying to update GridView selected record. Almost there but getting the error below. Full code attached.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Line 35:int id = Convert.ToInt32(GridView1.SelectedRow.Cells[0].Text.ToString());
Please help.
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["TrainingWebConnectionString"].ConnectionString;
string checkOleDb = "";
using (OleDbConnection myconnection = new OleDbConnection(connectionString))
{
using (OleDbCommand myCommand = new OleDbCommand(checkOleDb, myconnection))
{
int id = Convert.ToInt32(GridView1.SelectedRow.Cells[0].Text.ToString());
myCommand.CommandText = "UPDATE [TasksProject] SET [AsAtDate] = @DateTime WHERE [ID] = @id";
myCommand.Parameters.AddWithValue("@DateTime", DateTime.Now.ToString());
myconnection.Open();
myCommand.ExecuteNonQuery();
}
}
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Trainingbundle
{
public partial class TaskDevList2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
int id = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
string connectionString = ConfigurationManager.ConnectionStrings["TrainingWebConnectionString"].ConnectionString;
string checkOleDb = "";
using (OleDbConnection myconnection = new OleDbConnection(connectionString))
{
using (OleDbCommand myCommand = new OleDbCommand(checkOleDb, myconnection))
{
myCommand.CommandText = "UPDATE [TasksProject] SET [AsAtDate] = @DateTime WHERE [id] = @id";
myCommand.Parameters.AddWithValue("@DateTime", DateTime.Now.ToString());
myCommand.Parameters.AddWithValue("@id", id);
myconnection.Open();
myCommand.ExecuteNonQuery();
}
}
}
}
Member
17 Points
146 Posts
GridView selected record update
Dec 16, 2016 04:28 PM|asante_za|LINK
Trying to update GridView selected record. Almost there but getting the error below. Full code attached.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Line 35:int id = Convert.ToInt32(GridView1.SelectedRow.Cells[0].Text.ToString());
Please help.
Contributor
2340 Points
807 Posts
Re: GridView selected record update
Dec 16, 2016 04:43 PM|codemovement.pk|LINK
Avoid using ToString() because text is already string
use this
instead of this
Get more information: http://codemovement.pk
Thanks,
Member
17 Points
146 Posts
Re: GridView selected record update
Dec 16, 2016 04:51 PM|asante_za|LINK
Thanks, but still getting the same error message:
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Contributor
2340 Points
807 Posts
Re: GridView selected record update
Dec 16, 2016 05:05 PM|codemovement.pk|LINK
Actually i just noticed that you are doing the whole thing in wrong event method you have to do this OnSelectedIndexChanged of the GridView like this.
Get more information: http://codemovement.pk
Thanks,
Member
17 Points
146 Posts
Re: GridView selected record update
Dec 16, 2016 09:50 PM|asante_za|LINK
No error message, but AsAtDate not getting updated. Could you please spot what is missing!
Code attached.
Member
17 Points
146 Posts
Re: GridView selected record update
Dec 17, 2016 05:38 AM|asante_za|LINK
I have got it! at last!
Find full code attached.
James