CFanatic

Go Back   CFanatic > Programming > C# Programming

Join CFanatic Forum Now

Database is not being Updated - Only the Datatable is topic posted under C# Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 09-06-2010, 10:51 AM
Junior Member
 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
lahiruagar is on a distinguished road
| More
Database is not being Updated - Only the Datatable is

Hi guys!
I'm new to C Sharp and the forums. I have this problem where I have to build a project for an agriculture company. I am using datatables to populate the data. The data is read from the database just fine, but the problem arises when I try to save the data(update the table). However the datatable gets updated, but the database doesn't. I am using an object oriented approach, which is working just fine - trust me For coming up with this code, I used the Sams "Teach yourself" series. I need help asap, so any suggestions are appreciated. Thanks in advance...

The code of the addUser form is;
Code:
    public partial class Page2 : Page
    {
        SqlConnection editPDConn = new SqlConnection();
        SqlDataAdapter editPDAdapter;
        SqlCommandBuilder editPDCB;
        DataTable editPDDT = new DataTable();
        int counter, anyErrors;
        string line, Id;
        editPD editThisUser;

        public Page2()
        {
            InitializeComponent();
            editPDConn.ConnectionString =
                @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\AgTrainDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            editPDConn.Open();
            readUserFromFile();
            getInfo();
            displayInfo();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
        }
        
        private void reset_Click(object sender, RoutedEventArgs e)
        {
            ed_firstName.Text = "";
            ed_lastName.Text = "";
            ed_address.Text = "";
            ed_teleNumber.Text = "";
        }

        private void displayInfo()
        {
            ed_userName.Text = editThisUser.UserNameED.ToString();
            ed_firstName.Text = editThisUser.FirstNameED;
            ed_lastName.Text = editThisUser.LastNameED;
            ed_address.Text = editThisUser.AddressED;
            ed_teleNumber.Text = editThisUser.TeleED.ToString();
            ed_userRole.Text = editThisUser.RoleED;
        }

        //With the use of the retreived ID - the info is aquired here
        private void getInfo()
        {
            editThisUser = new editPD();
            editPDAdapter =
                            new SqlDataAdapter("SELECT * FROM users WHERE userId = '" + Id + "'", editPDConn);
            editPDCB =
                new SqlCommandBuilder(editPDAdapter);
            editPDAdapter.Fill(editPDDT);

            SqlCommandBuilder Builder = new SqlCommandBuilder(editPDAdapter);

            if (editPDDT.Rows.Count > 0)
            {
                editThisUser.UserIdED = (int)editPDDT.Rows[0]["userId"];
                editThisUser.UserNameED = editPDDT.Rows[0]["userName"].ToString();
                editThisUser.PasswordED = editPDDT.Rows[0]["userPassword"].ToString();
                editThisUser.FirstNameED = editPDDT.Rows[0]["userFirstName"].ToString();
                editThisUser.LastNameED = editPDDT.Rows[0]["userLastName"].ToString();
                editThisUser.AddressED = editPDDT.Rows[0]["userAddress"].ToString();
                editThisUser.TeleED = (int)editPDDT.Rows[0]["userTele"];
                editThisUser.RoleED = editPDDT.Rows[0]["userRole"].ToString();
            }
        }

        //Get the user ID from the text file so that his/her info can be displayed
        private void readUserFromFile()
        {
            counter = 0;
            //Id = 0;
            StreamReader fromFile = new StreamReader("E:\\loginInfo.txt");
            while ((line = fromFile.ReadLine()) != null)
            {
                if (counter == 2) //ID is in this line
                {
                    Id = line;

                }
                counter++;
            }

            fromFile.Close();

        }

        private void save_Click(object sender, RoutedEventArgs e)
        {
            int a;
            anyErrors = 0;

            if (ed_firstName.Text == "")
            {
                MessageBox.Show("Error! Please enter Your First Name!");
                anyErrors = 1;
            }
            else if (ed_lastName.Text == "")
            {
                MessageBox.Show("Error! Please enter Your Last Name!");
                anyErrors = 1;
            }
            else if (ed_address.Text == "")
            {
                MessageBox.Show("Error! Please enter Your Address!");
                anyErrors = 1;
            }
            else if (ed_teleNumber.Text == "")
            {
                MessageBox.Show("Error! Please enter Your Tele!");
                anyErrors = 1;
            }
            else if (ed_teleNumber.Text != "")
            {
                try
                {
                    a = int.Parse(ed_teleNumber.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error! Please enter a numeric Tele!");
                    anyErrors = 1;
                }
            }

            //Set new values to the object
            if (anyErrors != 1)
            {
                editThisUser.FirstNameED = ed_firstName.Text.ToString();
                editThisUser.LastNameED = ed_lastName.Text.ToString();
                editThisUser.AddressED = ed_address.Text.ToString();
                editThisUser.TeleED = int.Parse(ed_teleNumber.Text);
                saveDetails();
            }
        }

        //Saves the details by using the object
        private void saveDetails()
        {
            //editPDDT.Rows[0]["userId"] = editThisUser.UserIdED;
            //editPDDT.Rows[0]["userName"] = editThisUser.UserNameED;
            //editPDDT.Rows[0]["userPassword"] = editThisUser.PasswordED;
            editPDDT.Rows[0]["userFirstName"] = editThisUser.FirstNameED;
            editPDDT.Rows[0]["userLastName"] = editThisUser.LastNameED;
            editPDDT.Rows[0]["userAddress"] = editThisUser.AddressED;
            editPDDT.Rows[0]["userTele"] = editThisUser.TeleED;
            //editPDDT.Rows[0]["userRole"] = editThisUser.RoleED;
            //editPDAdapter.Update(editPDDT);
            this.editPDAdapter.Update(this.editPDDT); //This is where the data should get updated

            editPDConn.Close();
            editPDConn.Dispose();
        }

    }
}

Last edited by shabbir; 09-06-2010 at 12:02 PM. Reason: Code Blocks
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
web database expert_dee Looking to hire 4 01-21-2011 12:03 AM
Button.Text property not updated in compiled executable mavy-online C# Programming 0 09-05-2010 07:01 AM
convert data from Excell to dataset/datatable Dhammakirty C# Programming 0 08-31-2010 11:50 PM
DataTable does not match to any DataTable in source GunnarJ C# Programming 0 08-28-2010 03:00 AM
Show Updated Data In Listview bdtiger C# Programming 0 01-23-2009 02:33 PM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 04:51 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board