473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Editing Data using DataList and MySql

Hi,

I am trying to use a datalist to edit data in a MySql database as
follows. For now I just want to be able to get the changes to be made
on the dataset. Here is my code:

using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;

namespace eDen
{
/// <summary>
/// Summary description for administratorsinformation.
/// </summary>
public class administratorsinformation : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblRole;
protected System.Web.UI.WebControls.DataList dlTenants;
protected MySqlDataReader myReader = null;
protected DataSet myDataSet;
protected System.Web.UI.WebControls.DataList dlLandlords;
protected System.Web.UI.WebControls.DataList dlMaintenanceCompanies;
protected System.Web.UI.WebControls.DataList dlBOG;
protected MySqlConnection myConnection;
protected MySqlDataAdapter myDataAdapter;

private void Page_Load(object sender, System.EventArgs e)
{
lblRole.Text = "You are logged in as an Administrator.";
if (!IsPostBack) {
try
{
string strConn =
ConfigurationSettings.AppSettings["ConnectionString"];
myConnection = new MySqlConnection(strConn);
MySqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.CommandText = "procGetTenantsForAdmin";
myCommand.Parameters.Add("IN_ADMINUSERID", eDen.header.u.userid);
myCommand.Parameters["IN_ADMINUSERID"].Direction =
ParameterDirection.Input;
myDataAdapter = new MySqlDataAdapter(myCommand);
myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "Tenants");
dlTenants.DataBind();

}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
if (myReader != null)
myReader.Close();
myConnection.Close();
}
}
}

#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.dlTenants.CancelCommand += new
System.Web.UI.WebControls.DataListCommandEventHand ler(this.dlTenants_CancelCommand);
this.dlTenants.EditCommand += new
System.Web.UI.WebControls.DataListCommandEventHand ler(this.dlTenants_EditCommand);
this.dlTenants.UpdateCommand += new
System.Web.UI.WebControls.DataListCommandEventHand ler(this.dlTenants_UpdateCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dlTenants_CancelCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
dlTenants.EditItemIndex = -1;
myDataAdapter.Fill(myDataSet, "Tenants");
DataBind();
}

private void dlTenants_EditCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
try
{
dlTenants.EditItemIndex = e.Item.ItemIndex;
myDataAdapter.Fill(myDataSet,"Tenants");
DataBind();
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}

private void dlTenants_UpdateCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
HtmlInputText htEdit;
myDataAdapter.Fill(myDataSet, "Tenants");
htEdit = (HtmlInputText)e.Item.FindControl("txtfirstname");
// [2] because if you look at procGetTenantsForAdmin, firstname
// is the 3rd column. zero based.
myDataSet.Tables["Tenants"].Rows[e.Item.ItemIndex][2] =
htEdit.Value;
dlTenants.EditItemIndex = -1;
DataBind();
}

}
}
However, as soon as I click the Edit button, the code fails on the
following line inside EditComand handler:

myDataAdapter.Fill(myDataSet,"Tenants");

and the error I get is:

"System.NullReferenceException: Object reference not set to an instance
of an object."

I can't figure out what I'm doing wrong. Any ideas?

Thanks.

A

Dec 6 '05 #1
3 2192
Have you tried to instantiate the table directly?

Also...
Maybe you should try it with a reader to see it anything works...

Dec 6 '05 #2
Where do you want me to instantiate the table? And how?

What advantage would using a reader give me? The problem is not that I
can't read the data. The data displays fine, the problem occurs after I
hit the edit button.

Dec 6 '05 #3
I moved the following code out of page_load method and put it inside a
new void method called loaddata().

string strConn =
ConfigurationSettings.AppSettings["ConnectionString"];
myConnection = new
MySqlConnection(strConn);
MySqlCommand myCommand =
myConnection.CreateCommand();
myCommand.CommandType =
CommandType.StoredProcedure;
myCommand.CommandText =
"procGetTenantsForAdmin";

myCommand.Parameters.Add("IN_ADMINUSERID", eDen.header.u.userid);

myCommand.Parameters["IN_ADMINUSERID"].Direction =
ParameterDirection.Input;
myDataAdapter = new
MySqlDataAdapter(myCommand);
myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet,
"Tenants");
dlTenants.DataBind();

And called this method inside page_load, edit, cancel and update
methods. Apparently you need to make a new connection each time when
you're showing the data initially, when the user clicks the edit
button, if he presses cancel or update.

Asad

Dec 6 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alex | last post by:
Interested in more .NET stuff visit www.dedicatedsolutions.co.uk The DataList is not as powerful as the DataGrid. It requires more work from you since it has no default data presentation format....
1
by: Etienne Tremblay | last post by:
Hi, I'm currently trying to implement editing in a simple Datalist displaying news on my site. I did everything the walkthrought of microsoft said to do, but it doesn't seem to work. Just for...
5
by: Stephanie_Stowe | last post by:
Hi. I am trying to get used to AS.NET. I have been doing ASP classic for years, and am now in a position to do ASP.NET. I am in the stumbling around until I get my bearings phase. I hope you will...
0
by: Ryu | last post by:
Hi I have method that binds data to the DataList. void BindControls() { //Code Here DataList.DataBind(); }
9
by: tshad | last post by:
Is there a way to use your own image in place of the automatic one that ASP uses when doing editing in your DataGrid pages? We already have a style of button we are using and would like to be...
10
by: Nathan Sokalski | last post by:
I have a DataList control with an EditTemplate. Three of the controls in this template include a Calendar, a Button with CommandName="update", and a Button with CommandName="cancel". Whenever I...
1
by: Simon Cheng | last post by:
When I tried to display data in datalist and repeater, the first data item always disappears in all datalist and repeaters in my web forms. The code I use for the html page is: <TD...
0
by: HP | last post by:
Hi there I have a datalist control with some bound controls in its Item Template and a gridview bound to one of those fields (residing also in Item Template). I've found out that when I click...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.