1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Database is not being Updated - Only the Datatable is

Discussion in 'C#' started by lahiruagar, Sep 6, 2010.

  1. lahiruagar

    lahiruagar New Member

    Joined:
    Sep 6, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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();
            }
    
        }
    }
     

Share This Page