Hi guys,
I would like to ask your help about saving in ado.net. I was able to update it only on the display but when I check the database, it is not updated. I paste below my code and hopefully you can help me. - using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.Data;
-
using System.Data.SqlClient;
-
-
namespace ADOTest5
-
{
-
class Connect
-
{
-
//declare member var
-
private SqlConnection _con;
-
private SqlDataAdapter _da;
-
private DataSet _ds;
-
private DataTable _dt;
-
private string con_string;
-
private string com_string;
-
//private System.Windows.Forms.ListBox listBox;
-
-
public Connect()
-
{
-
con_string = global::ADOTest5.Properties.Settings.Default.MyDatabaseConnectionString;
-
com_string = "SELECT * FROM dbo.Customers";
-
-
//this.listBox = new System.Windows.Forms.ListBox();
-
}
-
-
public bool GetConnected()
-
{
-
try
-
{
-
_con = new SqlConnection(con_string);
-
_da = new SqlDataAdapter(com_string, con_string);
-
_con.Open();
-
-
//populate dataset
-
//_ds = new DataSet();
-
//_da.Fill(_ds, "Customers");
-
-
InitializeCommands();
-
}
-
catch(Exception ex)
-
{
-
Console.WriteLine("Connection Error" + ex.Message);
-
_con = null;
-
return false;
-
}
-
-
return true;
-
}
-
-
public void GetClosed()
-
{
-
if (_con != null)
-
{
-
_con.Close();
-
_con = null;
-
}
-
}
-
-
//fill the listbox
-
public void PopulateListBox(Object obj)
-
{
-
//populate dataset
-
_ds = new DataSet();
-
_da.Fill(_ds, "Customers");
-
-
System.Windows.Forms.ListBox listBox =
-
(System.Windows.Forms.ListBox)obj;
-
-
listBox.Items.Clear();
-
_dt = _ds.Tables["Customers"];
-
-
//Loop through the DataSet and add each row
-
// to the listbox
-
foreach (DataRow drow in _dt.Rows)
-
{
-
listBox.Items.Add(drow["Id"] + ", " + drow["Lname"] + ", " + drow["Fname"]);
-
}
-
-
//Prepare Commands
-
//InitializeCommands();
-
}
-
-
//Bind parameters to each column;params means variable # of param
-
public void AddParams(SqlCommand cmd, params string[] cols)
-
{
-
//add each parameter
-
cmd.Parameters.Add("@Id", SqlDbType.Int, 0, "Id");
-
cmd.Parameters.Add("@Fname", SqlDbType.NVarChar, 9, "Fname");
-
cmd.Parameters.Add("@Lname", SqlDbType.NVarChar, 9, "Lname");
-
//foreach (String column in cols)
-
//{
-
// cmd.Parameters.Add("@", SqlDbType.NVarChar, 0, column);
-
//}
-
}
-
-
public void InitializeCommands()
-
{
-
//prepare UpdateCommand
-
_da.UpdateCommand = _con.CreateCommand();
-
_da.UpdateCommand.CommandText =
-
"UPDATE [dbo].[Customers] SET [Fname] = @Fname, [Lname] = @Lname" +
-
" WHERE ([Id] = @Id)";
-
AddParams(_da.UpdateCommand, "Fname", "Lname");
-
}
-
-
-
// Fill the firsname/lastname textbox
-
public void FillInfo(Object obj1, Object obj2, int index)
-
{
-
// cast to textbox; for fname
-
System.Windows.Forms.TextBox Tbox1 =
-
(System.Windows.Forms.TextBox) obj1;
-
-
//for last name
-
System.Windows.Forms.TextBox Tbox2 =
-
(System.Windows.Forms.TextBox)obj2;
-
-
Tbox1.Text = _dt.Rows[index]["Fname"].ToString();
-
Tbox2.Text = _dt.Rows[index]["Lname"].ToString();
-
}
-
-
public void SaveChanges(Object obj1, Object obj2, int index)
-
{
-
// cast to textbox; for fname
-
System.Windows.Forms.TextBox Tbox1 =
-
(System.Windows.Forms.TextBox) obj1;
-
-
//for last name
-
System.Windows.Forms.TextBox Tbox2 =
-
(System.Windows.Forms.TextBox)obj2;
-
-
DataRow row = _dt.Rows[index];
-
row.BeginEdit();
-
row["Fname"] = Tbox1.Text;
-
row["Lname"] = Tbox2.Text;
-
row.EndEdit();
-
-
_da.Update(_ds, "Customers");
-
_ds.AcceptChanges();
-
}
-
}
-
}
-
-
-
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.Text;
-
using System.Windows.Forms;
-
-
namespace ADOTest5
-
{
-
public partial class Form1 : Form
-
{
-
Connect c;
-
int index = 0;
-
-
public Form1()
-
{
-
InitializeComponent();
-
c = new Connect();
-
}
-
-
private void LoadButton_Click(object sender, EventArgs e)
-
{
-
//Connect c = new Connect();
-
if (c.GetConnected())
-
{
-
c.PopulateListBox(this.listBox1);
-
c.GetClosed();
-
}
-
}
-
-
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
-
{
-
//display the index of selected items
-
//this.FirstNameTbox.Text = this.listBox1.SelectedIndex.ToString();
-
//int index = this.listBox1.SelectedIndex;
-
index = this.listBox1.SelectedIndex;
-
c.FillInfo(this.FirstNameTbox, this.LastNameTbox, index);
-
}
-
-
private void SaveButton_Click(object sender, EventArgs e)
-
{
-
if (c.GetConnected())
-
{
-
c.SaveChanges(this.FirstNameTbox, this.LastNameTbox, index);
-
c.PopulateListBox(this.listBox1);
-
}
-
c.GetClosed();
-
//c.SaveChanges(this.FirstNameTbox, this.LastNameTbox, index);
-
//c.PopulateListBox(this.listBox1);
-
}
-
}
-
}
1 1334
I believe that your update command is not called, seems like you are calling InitializeComma nds in the wrong place. HTH.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: annoyedtuna |
last post by:
I'm having nightmares with CSS at the moment. Can anyone help me out?
I'm including my stylesheet using the following code
<head>
<title>My Title.</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css"></link>
</head>
In IE this works fine
|
by: Bob Darlington |
last post by:
I have a repair and backup database routine which runs when a user closes
down my application.
It works fine in my development machine, but breaks on a client's at the
following line:
If Dir(strLDB) <> "" Then Kill (strLDB)
where strLDB is the path to the ldb file.
The client advises that the ldb doesn't lurk after the program closes.
Any ideas?
|
by: mimmo |
last post by:
I have a problem while calculating timezone (from Europe/Rome to
Brazil/East).
Can someone help me ??
Thank you,
Mimmo.
On this site
http://www.timezoneconverter.com/cgi-bin/tzc.tzc
I have for this date 2003/01/01 00:00:00:
|
by: Kidus Yared |
last post by:
I am having a problem displaying Unicode characters on my Forms labels and
buttons. After coding Button1.Text = unicode; where the unicode is a Unicode
character or string (‘\u1234’ or “\u1234”) It seems to work the first time I
set the button to the Unicode character. After a while, when saving my code,
I get a pop-up window stating that I need to save the file as a Unicode else
my changes will not be saved.
Seance I do not want...
|
by: Karl |
last post by:
Hi,
I have some code that will save the contents of a Rich Text Box in
either a Text or Rich Text Format file. The code is using the
SaveFileDialog and is working correctly.
I have been testing the code and added in some exception handling to
cater for any problems. During testing I have found that if I attempt
to save to a floppy disc that is full, a System.IO.IOException is raied
with the message "There is not enough space on the...
| |
by: IkBenHet |
last post by:
Hello,
I use this script to upload image files to a folder on a IIS6 server:
******************* START UPLOAD.ASPX FILE **********************
<%@ Page Language="VB" Debug="true" %>
|
by: Nathan Sokalski |
last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving, the other in which I close the FileStream afterwards, although both return the same error. Here are the two versions of the code and the errors they each return (NOTE: I rebooted immediately before running each of these versions so that I knew they...
|
by: Waqas.L.Khan |
last post by:
Hi guys,
I have a problem when trying to create an image file. Basically my
code takes any file and gets it's system icon using SHGetFileInfo and
then saves the file either by converting it into a bmp and saving to
disk or by saving the icon using a filestream. In both cases saving
the file works fine. However, when I open the newly created file from
explorer I notice something peculiar. The file has a black background.
I would have...
|
by: BobLewiston |
last post by:
When I try to save a new (inserted) record in an SQL database, I get the following System.Runtime.InteropServices.ExternalException message:
I have to either find out how to insert an appropriate value into the PasswordHash column OR make SQL Server Management Studio allow NULL in the PasswordHash column.
I discovered I could do the latter in SQL Server Management Studio via:
expand table | expand Columns | right-click PasswordHash...
|
by: BobLewiston |
last post by:
Some of you may have seen my earlier thread PasswordHash NULL problem. Ive started a new thread because investigation has shown that the problem is actually quite different than I previously stated. Also please note that this is unrelated to another of my previous threads, dataAdapter.Update problem, which incidentally has been resolved.
Im learning SQL. Im accessing database SQL2008 AdventureWorks, table Person.Contact, which has a...
|
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,...
| |
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
| |
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
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |