hi all,

i am currently writing a program as my school homework. it used to work and when i changed the column name in my database, it won't save data to the database. i am currently using sql server 2005 and visual studio 2008 and c# language.

here is my code, is there anything wrong with my code?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Data.SqlClient;
using System.Threading;
using System.Net.NetworkInformation;
using System.IO;

namespace WinFrm_FitnessCentral_Ver1._0
{
    public partial class Frm_UserRegistration : Form
    {
        public string Username, Password, FirstName, LastName, DOB, Address, Country, Email;

        private int pingsSent;

        AutoResetEvent resetEvent = new AutoResetEvent(false);

        public Frm_UserRegistration()
        {
            InitializeComponent();
        }

        DateTime currentTime = DateTime.Now;
        
        //private void button1_Click(object sender, EventArgs e)
        //{
            //DialogResult result = openFileDialog1.ShowDialog();  
            //if (result == DialogResult.OK)
                //PhotoBox.ImageLocation = openFileDialog1.FileName;        
        //}

        private void MetricCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (MetricCheckBox.Checked == true)
            {
                CmLabel.Visible = true;
                KgLabel.Visible = true;
                PoundsLabel.Visible = false;
                InchesLabel.Visible = false;
            }
            else
            {
                CmLabel.Visible = false;
                KgLabel.Visible = false;
                PoundsLabel.Visible = true;
                InchesLabel.Visible = true;
            }
        }

        private void Frm_UserRegistration_Load(object sender, EventArgs e)
        {
            ReadOnlyCollection<TimeZoneInfo> tzCollection;
            tzCollection = TimeZoneInfo.GetSystemTimeZones();
            this.TimeZoneCombo.DataSource = tzCollection;
            TimeZoneInfo country = TimeZoneInfo.Local;

            TimeZone localZone = TimeZone.CurrentTimeZone;

            int currentYear = currentTime.Year;

            DateTime currentUTC = localZone.ToUniversalTime(currentTime);
            TimeSpan currentOffset = localZone.GetUtcOffset(currentTime);

            if (TimeZoneCombo.SelectedItem.ToString() != country.ToString())
            {
                TimeZoneCombo.Text = country.ToString();
            }
        }

        private void UsernameTextBox_TextChanged(object sender, EventArgs e)
        {
            this.Username = this.UsernameTextBox.Text.ToString();
        }

        private void PasswordTextBox_TextChanged(object sender, EventArgs e)
        {
            this.Password = this.PasswordTextBox.Text.ToString();
        }

        private void FirstNameTextBox_TextChanged(object sender, EventArgs e)
        {
            this.FirstName = this.FirstNameTextBox.Text.ToString();
        }

        private void LastNameTextBox_TextChanged(object sender, EventArgs e)
        {
            this.LastName = this.LastNameTextBox.Text.ToString();
        }

        private void DOBTextBox_TextChanged(object sender, EventArgs e)
        {
            this.DOB = this.DOBTextBox.Text.ToString();
        }

        private void HouseAddressTextBox_TextChanged(object sender, EventArgs e)
        {
            this.Address = this.HouseAddressTextBox.Text.ToString();
        }

        private void CountryTextBox_TextChanged(object sender, EventArgs e)
        {
            this.Country = this.CountryTextBox.Text.ToString();
        }

        private void EmailTextBox_TextChanged(object sender, EventArgs e)
        {
            this.Email = this.EmailTextBox.Text.ToString();
        }

        private void RegisterButton_Click(object sender, EventArgs e)
        {
            TimeZoneInfo selectedTimeZone = (TimeZoneInfo)this.TimeZoneCombo.SelectedItem;

            pingsSent = 0;

            Ping();
        }

        private void EraseButton_Click(object sender, EventArgs e)
        {
            UsernameTextBox.Text = "";
            PasswordTextBox.Text = "";
            FirstNameTextBox.Text = "";
            LastNameTextBox.Text = "";
            DOBTextBox.Text = "";
            HouseAddressTextBox.Text = "";
            CountryTextBox.Text = "";
            EmailTextBox.Text = "";
        }

        private void Ping()
        {
            System.Net.NetworkInformation.Ping pingSender = new System.Net.NetworkInformation.Ping();

            pingSender.PingCompleted += new PingCompletedEventHandler(pingSender_Complete);

            byte[] packetData = Encoding.ASCII.GetBytes("--------------------------------");

            PingOptions packetOptions = new PingOptions(50, true);

            pingSender.SendAsync("175.16.227.57", 5000, packetData, packetOptions, resetEvent);
        }

        private void pingSender_Complete(object sender, PingCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                ((AutoResetEvent)e.UserState).Set();
            }
            else if (e.Error != null)
            {
                ((AutoResetEvent)e.UserState).Set();
            }
            else
            {
                PingReply pingResponse = e.Reply;

                ShowPingResults(pingResponse);
            }
        }

        public void ShowPingResults(PingReply pingResponse)
        {
            if (pingResponse == null)
            {
                MessageBox.Show("Network Connection is Down.");
            }
            else if (pingResponse.Status == IPStatus.Success)
            {
                SqlConnection Connection = new SqlConnection(Properties.Settings.Default.FitnessCentralDatabaseConnectionString);
                Connection.Open();
                SqlCommand InsertCommand = new SqlCommand
                ("INSERT INTO Customers (User_Name, Password, First_Name, Last_Name, Date_Of_Birth, Address, Country, Email_Address, LastUpdatedDate, CreationDate) VALUES ('" + this.Username + "','" + this.Password + "','" + this.FirstName + "','" + this.LastName + "','" + this.DOB + "','" + this.Address + "','" + this.Country + "','" + this.Email + "','" + DateTime.Now + "','" + DateTime.Now + "')",
                Connection);

                InsertCommand.ExecuteNonQuery();
                Connection.Close();

                MessageBox.Show("New Record Inserted. Please Check Table Data.");
            }
            else
            {
                StreamWriter Writer = new StreamWriter
                    ("D:\\\\15122009\\\\WinFrm_FitnessCentral_Ver1.0CorrectedNetwork\\\\USBDeviceData.txt");

                Writer.WriteLine(UsernameTextBox.Text);
                Writer.WriteLine(PasswordTextBox.Text);
                Writer.WriteLine(FirstNameTextBox.Text);
                Writer.WriteLine(LastNameTextBox.Text);
                Writer.WriteLine(DOBTextBox.Text);
                Writer.WriteLine(HouseAddressTextBox.Text);
                Writer.WriteLine(CountryTextBox.Text);
                Writer.WriteLine(EmailTextBox.Text);

                Writer.Close();

                MessageBox.Show("Network Connection Timed Out.");
            }
            pingsSent++;

            if (pingsSent < 1)
            {
                Ping();
            }
        }
    }
}

u said the only change tht u have made is the name of the column in ur database.
i hope u have also changed the name of the column in your code as well..
for example in the insert query u shud change the name of the corresponding column name and it shud match with ur database.

What do you mean by it wont save data? Does the code throw an exception or does it not error, but you don't see data?

i have changed the code in the program too and it doesn't work. my form just disappeared after i entered the data and hit the submit button. and when i checked the table where i inserted the data, there are no new records. i just now tried out the code and found out that the problem lies in this part:

InsertCommand.ExecuteNonQuery();

it is because i added a MessageBox.Show before this line, the message box appeared and the form disappeared quickly after i click OK.
but when i inserted the MessageBox.Show after this line, the message box didn't appear and the form disappeared after i submit the form.

Which column name did you change? Have you ensured that the column names are definitely matching?
Check in the output window in VS, are any exceptions listed when the form dissappears?

i have changed the LastEditDate Column to LastUpdatedDate. i can ensure that the column names are matching. there isn't any exceptions listed when the form disappears. the output window is empty. i've tried out yesterday again, changing it back the column to how it was, it still cannot work. it seems that there is something wrong with the ExecuteNonQuery line.

is this the complete code or have you missed some portion of it.

i have pasted the whole code of the registration form here. but there are also other forms attached to the project.

may i know if there is any other way where i can insert data without using the ExecuteNonQuery?

Upload your project. Something else is broken on it that you haven't outlined here.

i have attached my project to this thread. i tried to upload through the thread. but it seems to fail everytime. so i uploaded it sendspace.
here is the link:
http://www.sendspace.com/file/pzn0qm

my project is supposed to pop up a registration form when i plug in my usb device. when i run my project, there will be an icon running in the system tray and once, i plug in my usb device, the registration form will pop up.

that part works for me. the registration form will appear when i insert my usb device. it just won't get pass through the line of the code in the registration form:

InsertCommand.ExecuteNonQuery();

thanks for your help, everyone.

Can you check somethign for me? Insert a breakpoint at the line that crashes and check (before you attempt the ExecuteNonQuery) that
a) the InsertCommand contains the correct values
b) the connection opens correctly

i have inserted a breakpoint at the Connection.Open() line and i moved over the line after i have run the program. it shows that the connection is closed.
but when i insert a breakpoint at the InsertCommand.ExecuteNonQuery() line and i moved my cursor over the line after i've run the program, it shows that the connection is open.

and are all the values set correctly in your insert command? No null values, all values in correct format etc. You havent changed the data type of any of your database columns have you?

hi all, i have made the program work. i removed all the connections in my visual studio and deleted all the database that is in the sql server management studio in my sql server 2005. then, i tried attaching the sql database back to sql server management studio and re-create the connection in the visual studio and it worked and was able to insert data into the table.

thanks for your help and suggestions, everyone.
i am grateful to your help. =)

I'm glad you figured out the issue

Please mark this thread as solved (since you answered your own question :P) and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.