4 Replies - 3168 Views - Last Post: 11 July 2012 - 05:44 AM Rate Topic: -----

#1 Flatlander  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-July 12

I can not get my application to write to the database

Posted 10 July 2012 - 07:32 PM

I have been searching for hours and cannot fid the answer to this issue. Bear with me I am a noob but here is 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.Data.SqlClient;

namespace JambooziesCuctomerTracker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void RegisterButton_Click(object sender, EventArgs e)
        {
            //validate that information is present
            if (fname.Text == "")
            {
                MessageBox.Show("Please enter your first name");
            }
            else if (lname.Text == "")
            {
                MessageBox.Show("Please enter your last name");
            }
            else if (email.Text == "")
            {
                MessageBox.Show("Please enter a valid email address");
            }

            else
            {
                //save data in the textbox as a variable
                string FName = fname.Text;
                string LName = lname.Text;
                string Email = email.Text;
                string CellPhone = cell.Text;

                //establish DB connection
                SqlConnection conn;
                SqlCommand cmd;
                //SqlDataReader datareader;
                string sql;

                try
                {
                    //insert query
                    sql = "INSERT INTO CUSTOMER(FNAME, LNAME, EMAIL, CELL )";
                    sql += String.Format("VALUES (@FName, @LName, @Email, @CellPhone)");

                    conn = new SqlConnection(Properties.Settings.Default.JBCUSTDBDataset);
                    cmd = new SqlCommand(sql, conn);
                    cmd.CommandText = sql;
                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.AddWithValue("@FName", fname.Text);
                    cmd.Parameters.AddWithValue("@LName", lname.Text);
                    cmd.Parameters.AddWithValue("@Email", email.Text);
                    cmd.Parameters.AddWithValue("@CellPhone", cell.Text);
                
                    //open connection
                    conn.Open();

                    //write to DB
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Registration Successful");
                }
                catch (SqlException ex)
                {
                    
                    throw new Exception("Error Inserting", ex);
                }

                //close connection
                conn.Close();

            }
        }
    }
}




any help is greatly appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: I can not get my application to write to the database

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3421
  • View blog
  • Posts: 10,535
  • Joined: 05-May 12

Re: I can not get my application to write to the database

Posted 10 July 2012 - 09:48 PM

What's the issue you are trying to find an answer to?
Was This Post Helpful? 0
  • +
  • -

#3 zeeshanef  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 71
  • Joined: 14-April 12

Re: I can not get my application to write to the database

Posted 11 July 2012 - 01:40 AM

what error you receive? [ex.Message]
Was This Post Helpful? 0
  • +
  • -

#4 Flatlander  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 10-July 12

Re: I can not get my application to write to the database

Posted 11 July 2012 - 04:20 AM

Skydiver
The issue is the application doesnt wriet the data in the form to the database

Zeeshanef
There is no error thrown the application gives me the "Registration successful" message but does not write to the database
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5417
  • View blog
  • Posts: 11,609
  • Joined: 02-June 10

Re: I can not get my application to write to the database

Posted 11 July 2012 - 05:44 AM

rookie: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2 (debugging) & 26 (databases). (Click the SHOW button below)



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1