473,732 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Configur ation;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using MySql.Data.MySq lClient;

namespace eDen
{
/// <summary>
/// Summary description for administratorsi nformation.
/// </summary>
public class administratorsi nformation : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Labe l lblRole;
protected System.Web.UI.W ebControls.Data List dlTenants;
protected MySqlDataReader myReader = null;
protected DataSet myDataSet;
protected System.Web.UI.W ebControls.Data List dlLandlords;
protected System.Web.UI.W ebControls.Data List dlMaintenanceCo mpanies;
protected System.Web.UI.W ebControls.Data List dlBOG;
protected MySqlConnection myConnection;
protected MySqlDataAdapte r myDataAdapter;

private void Page_Load(objec t sender, System.EventArg s e)
{
lblRole.Text = "You are logged in as an Administrator." ;
if (!IsPostBack) {
try
{
string strConn =
ConfigurationSe ttings.AppSetti ngs["ConnectionStri ng"];
myConnection = new MySqlConnection (strConn);
MySqlCommand myCommand = myConnection.Cr eateCommand();
myCommand.Comma ndType = CommandType.Sto redProcedure;
myCommand.Comma ndText = "procGetTenants ForAdmin";
myCommand.Param eters.Add("IN_A DMINUSERID", eDen.header.u.u serid);
myCommand.Param eters["IN_ADMINUSERID "].Direction =
ParameterDirect ion.Input;
myDataAdapter = new MySqlDataAdapte r(myCommand);
myDataSet = new DataSet();
myDataAdapter.F ill(myDataSet, "Tenants");
dlTenants.DataB ind();

}
catch (MySql.Data.MyS qlClient.MySqlE xception ex)
{
Console.WriteLi ne(ex.ToString( ));
}
finally
{
if (myReader != null)
myReader.Close( );
myConnection.Cl ose();
}
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.dlTenants. CancelCommand += new
System.Web.UI.W ebControls.Data ListCommandEven tHandler(this.d lTenants_Cancel Command);
this.dlTenants. EditCommand += new
System.Web.UI.W ebControls.Data ListCommandEven tHandler(this.d lTenants_EditCo mmand);
this.dlTenants. UpdateCommand += new
System.Web.UI.W ebControls.Data ListCommandEven tHandler(this.d lTenants_Update Command);
this.Load += new System.EventHan dler(this.Page_ Load);

}
#endregion

private void dlTenants_Cance lCommand(object source,
System.Web.UI.W ebControls.Data ListCommandEven tArgs e)
{
dlTenants.EditI temIndex = -1;
myDataAdapter.F ill(myDataSet, "Tenants");
DataBind();
}

private void dlTenants_EditC ommand(object source,
System.Web.UI.W ebControls.Data ListCommandEven tArgs e)
{
try
{
dlTenants.EditI temIndex = e.Item.ItemInde x;
myDataAdapter.F ill(myDataSet," Tenants");
DataBind();
}
catch (Exception ex)
{
Console.Write(e x.ToString());
}
}

private void dlTenants_Updat eCommand(object source,
System.Web.UI.W ebControls.Data ListCommandEven tArgs e)
{
HtmlInputText htEdit;
myDataAdapter.F ill(myDataSet, "Tenants");
htEdit = (HtmlInputText) e.Item.FindCont rol("txtfirstna me");
// [2] because if you look at procGetTenantsF orAdmin, firstname
// is the 3rd column. zero based.
myDataSet.Table s["Tenants"].Rows[e.Item.ItemInde x][2] =
htEdit.Value;
dlTenants.EditI temIndex = -1;
DataBind();
}

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

myDataAdapter.F ill(myDataSet," Tenants");

and the error I get is:

"System.NullRef erenceException : 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 2221
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 =
ConfigurationSe ttings.AppSetti ngs["ConnectionStri ng"];
myConnection = new
MySqlConnection (strConn);
MySqlCommand myCommand =
myConnection.Cr eateCommand();
myCommand.Comma ndType =
CommandType.Sto redProcedure;
myCommand.Comma ndText =
"procGetTenants ForAdmin";

myCommand.Param eters.Add("IN_A DMINUSERID", eDen.header.u.u serid);

myCommand.Param eters["IN_ADMINUSERID "].Direction =
ParameterDirect ion.Input;
myDataAdapter = new
MySqlDataAdapte r(myCommand);
myDataSet = new DataSet();
myDataAdapter.F ill(myDataSet,
"Tenants");
dlTenants.DataB ind();

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
3112
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. However, the DataGrid begins to get very cumbersome as the number of columns of data you present increases. Anything more than half a dozen columns or so and you probably induce horizontal scrolling - a real no-no for me. If you put such a...
1
1559
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 a matter of debugguing, I maintained a list of what event is called in a session variable because I lost way to much time on this and wanted to know what was happening... When I press an Edit or Select button in my <ItemTemplate>, the event is...
5
2630
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 bear with me. I am going through the QuickStart. After reading a little, I am trying to implement a simple page on a simple project I have made up. I have a page called default.aspx. I want it to load a list of user names from a SQL database...
0
1053
by: Ryu | last post by:
Hi I have method that binds data to the DataList. void BindControls() { //Code Here DataList.DataBind(); }
9
4608
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 consistant. Thanks, Tom.
10
1498
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 click one of these controls, the DataList replaces the EditTemplate with the ItemTemplate. I think this is because the method I wrote which performs the databinding is getting called, but I don't know why or where it is getting called from. It does...
1
1747
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 id="tdDepartment" runat="server"><asp:datalist id="dlstDept" runat="server" HorizontalAlign="Left" CellPadding="0" Width="100%"> <HeaderTemplate> <p><b>Department Info</b></p> </HeaderTemplate> <ItemTemplate>
0
1467
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 Edit on the gridview (same happens when editing other gridview which is outside the datalist) all datalist bound fields (except the gridview) disappear, and they don't appear after submitting GV changes - they do only after changing value of some
1
3507
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 the ArrayList back from pgm3 how to extract and separate the fields out fo the ArrayLists? Question2: When pgm3 gets ArrayList back from pgm2 how to separate the
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.