Reputation: 3247
I am trying to populate a data table from a database table. After it is populated i am using asp:GridView to show the data.
The problem is I am trying to edit some rows after it is populated from the database but before binding the data.
I am very new to c# so i cant get it working. My code does not update anything.
private void BindGridviewFileData()
{
string ConnectionStringName = WebConfigurationManager.ConnectionStrings["LocalConnection"].ConnectionString;
try
{
using (SqlConnection sqlConn = new SqlConnection(ConnectionStringName))
{
using (SqlCommand sqlCmd = new SqlCommand())
{
sqlCmd.CommandText = "SELECT * FROM VISUALISATION";
sqlCmd.Connection = sqlConn;
sqlConn.Open();
SqlDataReader objDataReader = sqlCmd.ExecuteReader();
DataTable objDataTable = new DataTable();
objDataTable.Load(objDataReader);
foreach (DataRow row in objDataTable.Rows)
if (row["lineNumber"] == "1")
row["BATCH_NO"] = "new Value";
gvSubjectDetails.DataSource = objDataTable;
gvSubjectDetails.DataBind();
sqlConn.Close();
}
}
}
catch { }
}
Upvotes: 1
Views: 244
Reputation: 1063328
The lineNumber
check probably needs to be an int
check; note that currently it is actually doing an object
check, which only supports reference equality - it will never match, even if the value is a string
of "1"
, because the value from the database will become a different string
instance than the interned literal "1"
from the code. So, the first check probably needs to be:
if((int)row["lineNumber"] == 1)
You ask in comments how to do this with a string; in that case, quite similarly:
if((string)row["BATCH_NO"] == "some value")
This now works because there is an overloaded ==
operator for string==string
, which does a value based equality test, not a reference-based equality test.
Note: you could also probably use:
if(Equals(row["BATCH_NO"], "some value"))
but I find the earlier version clearer.
Upvotes: 2