I am trying to create a portal of sorts and I'm very new to asp.net but not to programming in general. This is supposed to simply update the dataSet (which is a global DataSet) with the information in a form and it gives me an error when I try to access a row in the table (index is also a global variable (int)) I've used the same exact code in another function and it works fine, any ideas?
Thanks,
Kasper
Hi,
public void btnSubmit_Click(object sender, EventArgs e)
{
string edTitle = txtTitle.Value;
string edArticle = txtArticle.Value;
string edDate = DateTime.Now.Month + "." + DateTime.Now.Day + "." + DateTime.Now.Year;DataTable dataTable = dataSet.Tables["articles"];
DataRow edRow = dataTable.Rows[index]; <--Error here
edRow["article"] = edArticle;
edRow["title"] = edTitle;
edRow["date"] = edDate;dgUpdatedArticles.DataSource = dataTable;
dgUpdatedArticles.DataBind( );
pnlUpdatedArticles.Visible = true;}
Try printing the value of Index within the function.
I hope it is getting null value.
thanks.
Where are you declaring 'index'? is it a global variable?
Yes, it is global, I'm declaring it at the top of the class outside of any functions.
On top of that, I've tried putting 0 in for the Rows value but it still does nothing.
Here's the rest of the relevant code if you care.
protected System.Web.UI.WebControls.DataGrid dgArticles;
protected System.Web.UI.WebControls.DataGrid dgUpdatedArticles;
protected System.Web.UI.WebControls.Panel pnlUpdatedArticles;
protected System.Web.UI.WebControls.Panel pnlUpdateForm;
protected System.Web.UI.WebControls.Panel pnlPreview;
protected System.Web.UI.WebControls.Button btnSubmit;
protected System.Web.UI.WebControls.Button btnPreview;
protected System.Web.UI.WebControls.Label lblTitle;
protected System.Web.UI.WebControls.Label lblArticle;
protected System.Web.UI.WebControls.Label lblDate;
protected System.Web.UI.HtmlControls.HtmlInputText txtTitle;
protected System.Web.UI.HtmlControls.HtmlTextArea txtArticle;DataSet dataSet = new DataSet( );
DataSet origDataSet = new DataSet( );
int index = 0;public void Page_Load(Object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string strConnection = "driver={MySQL ODBC 3.51 Driver}; server=localhost; uid=kasper; pwd=blacksun; database=kasper_uf_db";string strCommand = "SELECT * FROM articles ORDER BY id DESC";
OdbcDataAdapter dataAdapter_Load = new OdbcDataAdapter(strCommand, strConnection);
DataSet dataSet_Load = new DataSet( );
dataAdapter_Load.Fill(dataSet_Load, "articles");
OdbcCommandBuilder bldr = new OdbcCommandBuilder(dataAdapter_Load);DataTable dataTable_Load = dataSet_Load.Tables[0];
dgArticles.DataSource = dataTable_Load;
dgArticles.DataBind( );
Response.Write("!IsPostBack\n<br />\n<br />");
} else {
Response.Write("IsPostBack\n<br />\n<br />");
}/*string strConn = "driver={MySQL ODBC 3.51 Driver}; server=localhost; uid=kasper; pwd=blacksun; database=kasper_uf_db";
*OdbcConnection objConn = new OdbcConnection(strConn);
*objConn.Open();
*
*string strComm = "SELECT article FROM articles ORDER BY id DESC";
*OdbcCommand objComm = new OdbcCommand(strComm, objConn );
*
*OdbcDataReader objDataReader = objComm.ExecuteReader( );
*
*while(objDataReader.Read())
*{
*string article = (string) objDataReader["article"];
*Response.Write(article);
*Response.Write("\n<br />\n<br />\n<br />\n<br />\n");
*}*/
}public void SelectedIndexChanged_Handler(object sender, EventArgs e)
{
pnlPreview.Visible = false;
index = (int) dgArticles.SelectedIndex;
if(index != -1)
{
int articleID = (int) dgArticles.DataKeys[index];string strConn = "driver={MySQL ODBC 3.51 Driver}; server=localhost; uid=kasper; pwd=blacksun; database=kasper_uf_db";
OdbcConnection connection = new OdbcConnection(strConn);
connection.Open( );OdbcCommand command = new OdbcCommand( );
command.Connection = connection;
command.CommandText = "SELECT * FROM articles WHERE id=" + articleID;OdbcDataAdapter dataAdapter = new OdbcDataAdapter( );
dataAdapter.SelectCommand = command;
dataAdapter.TableMappings.Add("Table", "articles");
dataAdapter.Fill(origDataSet);
dataSet = origDataSet;DataTable dataTable = origDataSet.Tables["articles"];
DataRow articleRow = dataTable.Rows[0];
string title = (string) articleRow["title"];
string article = (string) articleRow["article"];txtTitle.Value = title;
txtArticle.Value = article;
pnlUpdateForm.Visible = true;
}
}public void btnPreview_Click(object sender, EventArgs e)
{
string prvwTitle = txtTitle.Value;
string prvwArticle = txtArticle.Value;
string today = DateTime.Now.Month + "." + DateTime.Now.Day + "." + DateTime.Now.Year;lblTitle.Text = prvwTitle;
lblDate.Text = today;
lblArticle.Text = prvwArticle;pnlPreview.Visible = true;
}
public void btnSubmit_Click(object sender, EventArgs e)
{
string updateTitle = txtTitle.Value;
string updateArticle = txtArticle.Value;
string updateDate = DateTime.Now.Month + "." + DateTime.Now.Day + "." + DateTime.Now.Year;DataTable newDataTable = dataSet.Tables["articles"];
DataRow updateRow = newDataTable.NewRow( );
updateRow["article"] = updateArticle;
updateRow["title"] = updateTitle;
updateRow["date"] = updateDate;dgUpdatedArticles.DataSource = newDataTable;
dgUpdatedArticles.DataBind( );
pnlUpdatedArticles.Visible = true;}
I realize that there are some issues with how I'm handling data etc. but like I said, I'm new to ASP.NET and this is just the beginning of my article portal, once I get over this hurdle I will fix those things.
Thanks,
Kasper
Hi,
try, dataset.Tables.Count to make sure your dataset is being filled properly
Thanks jimiz, that worked. It returned 0. My best guess is that even though it's global, its value is erased at the end of the method in which it is set. I have it setup so that origDataSet and dataSet are initialized as 'new DataSet( );' so they are global, then, when they select an article, the DataSets are filled with the info for the article and a table from one of the DataSets is bound to a datagrid. Then later, in this btnSubmit_Click() function, the other DataSet is updated with their changes and is bound to another DataGrid that has the updated articles in it.
So basically, on DataSet holds the original data, and one holds the updated data, and they are both assigned to different DataGrids.
Any ideas why both DataSets come up as empty in this function? I know that the function that fills them is being called before this one, so why aren't they filled?
Thanks,
Kasper
Nothing? Nothing at all?
Just guessing
DataRow edRow = dataTable.Rows[index]; <--Error here
To
DataRow edRow = dataTable["articles"].Rows[index];
0 comments:
Post a Comment