473,402 Members | 2,046 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,402 software developers and data experts.

Problem in Saving in ADO.Net

108 100+
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.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6.  
  7. namespace ADOTest5
  8. {
  9.     class Connect
  10.     {
  11.         //declare member var
  12.         private SqlConnection _con;
  13.         private SqlDataAdapter _da;
  14.         private DataSet _ds;
  15.         private DataTable _dt;
  16.         private string con_string;
  17.         private string com_string;
  18.         //private System.Windows.Forms.ListBox listBox;
  19.  
  20.         public Connect()
  21.         {
  22.             con_string = global::ADOTest5.Properties.Settings.Default.MyDatabaseConnectionString;
  23.             com_string = "SELECT * FROM dbo.Customers";
  24.  
  25.             //this.listBox = new System.Windows.Forms.ListBox();
  26.         }
  27.  
  28.         public bool GetConnected()
  29.         {
  30.             try
  31.             {
  32.                 _con = new SqlConnection(con_string);
  33.                 _da = new SqlDataAdapter(com_string, con_string);
  34.                 _con.Open();
  35.  
  36.                 //populate dataset
  37.                 //_ds = new DataSet();
  38.                 //_da.Fill(_ds, "Customers");
  39.  
  40.                 InitializeCommands();
  41.             }
  42.             catch(Exception ex)
  43.             {
  44.                 Console.WriteLine("Connection Error" + ex.Message);
  45.                 _con = null;
  46.                 return false;
  47.             }
  48.  
  49.             return true;
  50.         }
  51.  
  52.         public void GetClosed()
  53.         {
  54.             if (_con != null)
  55.             {
  56.                 _con.Close();
  57.                 _con = null;
  58.             }
  59.         }
  60.  
  61.         //fill the listbox
  62.         public void PopulateListBox(Object obj)
  63.         {
  64.             //populate dataset
  65.             _ds = new DataSet();
  66.             _da.Fill(_ds, "Customers");
  67.  
  68.             System.Windows.Forms.ListBox listBox =
  69.                 (System.Windows.Forms.ListBox)obj;
  70.  
  71.             listBox.Items.Clear();
  72.             _dt = _ds.Tables["Customers"];
  73.  
  74.             //Loop through the DataSet and add each row 
  75.             // to the listbox
  76.             foreach (DataRow drow in _dt.Rows)
  77.             {
  78.                 listBox.Items.Add(drow["Id"] + ", " + drow["Lname"] + ", " + drow["Fname"]);
  79.             }
  80.  
  81.             //Prepare Commands
  82.             //InitializeCommands();
  83.         }
  84.  
  85.         //Bind parameters to each column;params means variable # of param
  86.         public void AddParams(SqlCommand cmd, params string[] cols)
  87.         {
  88.             //add each parameter
  89.             cmd.Parameters.Add("@Id", SqlDbType.Int, 0, "Id");
  90.             cmd.Parameters.Add("@Fname", SqlDbType.NVarChar, 9, "Fname");
  91.             cmd.Parameters.Add("@Lname", SqlDbType.NVarChar, 9, "Lname");
  92.             //foreach (String column in cols)
  93.             //{
  94.             //    cmd.Parameters.Add("@", SqlDbType.NVarChar, 0, column);
  95.             //}
  96.         }
  97.  
  98.         public void InitializeCommands()
  99.         {
  100.             //prepare UpdateCommand
  101.             _da.UpdateCommand = _con.CreateCommand();
  102.             _da.UpdateCommand.CommandText =
  103.                 "UPDATE [dbo].[Customers] SET [Fname] = @Fname, [Lname] = @Lname" +
  104.                 " WHERE ([Id] = @Id)";
  105.             AddParams(_da.UpdateCommand, "Fname", "Lname");
  106.         }
  107.  
  108.  
  109.         // Fill the firsname/lastname textbox
  110.         public void FillInfo(Object obj1, Object obj2, int index)
  111.         {
  112.             // cast to textbox; for fname
  113.             System.Windows.Forms.TextBox Tbox1 = 
  114.                 (System.Windows.Forms.TextBox) obj1;
  115.  
  116.             //for last name
  117.             System.Windows.Forms.TextBox Tbox2 =
  118.                 (System.Windows.Forms.TextBox)obj2;
  119.  
  120.             Tbox1.Text = _dt.Rows[index]["Fname"].ToString();
  121.             Tbox2.Text = _dt.Rows[index]["Lname"].ToString();
  122.         }
  123.  
  124.         public void SaveChanges(Object obj1, Object obj2, int index)
  125.         {
  126.             // cast to textbox; for fname
  127.             System.Windows.Forms.TextBox Tbox1 = 
  128.                 (System.Windows.Forms.TextBox) obj1;
  129.  
  130.             //for last name
  131.             System.Windows.Forms.TextBox Tbox2 =
  132.                 (System.Windows.Forms.TextBox)obj2;
  133.  
  134.             DataRow row = _dt.Rows[index];
  135.             row.BeginEdit();
  136.             row["Fname"] = Tbox1.Text;
  137.             row["Lname"] = Tbox2.Text;
  138.             row.EndEdit();
  139.  
  140.             _da.Update(_ds, "Customers");
  141.             _ds.AcceptChanges();
  142.         }
  143.     }
  144. }
  145.  
  146.  
  147.  
  148. using System;
  149. using System.Collections.Generic;
  150. using System.ComponentModel;
  151. using System.Data;
  152. using System.Drawing;
  153. using System.Text;
  154. using System.Windows.Forms;
  155.  
  156. namespace ADOTest5
  157. {
  158.     public partial class Form1 : Form
  159.     {
  160.         Connect c;
  161.         int index = 0;
  162.  
  163.         public Form1()
  164.         {
  165.             InitializeComponent();
  166.             c = new Connect();
  167.         }
  168.  
  169.         private void LoadButton_Click(object sender, EventArgs e)
  170.         {
  171.             //Connect c = new Connect();
  172.             if (c.GetConnected())
  173.             {
  174.                 c.PopulateListBox(this.listBox1);
  175.                 c.GetClosed();
  176.             }
  177.         }
  178.  
  179.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  180.         {
  181.             //display the index of selected items
  182.             //this.FirstNameTbox.Text = this.listBox1.SelectedIndex.ToString();
  183.             //int index = this.listBox1.SelectedIndex;
  184.             index = this.listBox1.SelectedIndex;
  185.             c.FillInfo(this.FirstNameTbox, this.LastNameTbox, index);
  186.         }
  187.  
  188.         private void SaveButton_Click(object sender, EventArgs e)
  189.         {
  190.             if (c.GetConnected())
  191.             {
  192.                 c.SaveChanges(this.FirstNameTbox, this.LastNameTbox, index);
  193.                 c.PopulateListBox(this.listBox1);
  194.             }
  195.             c.GetClosed();
  196.             //c.SaveChanges(this.FirstNameTbox, this.LastNameTbox, index);
  197.             //c.PopulateListBox(this.listBox1);
  198.         }
  199.     }
  200. }
Feb 8 '08 #1
1 1318
kenobewan
4,871 Expert 4TB
I believe that your update command is not called, seems like you are calling InitializeCommands in the wrong place. HTH.
Feb 8 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
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"...
13
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...
1
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...
3
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...
5
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...
5
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" %>
7
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,...
0
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...
6
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...
2
by: BobLewiston | last post by:
Some of you may have seen my earlier thread “PasswordHash NULL problem”. I’ve started a new thread because investigation has shown that the problem is actually quite different than I previously...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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.