Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

INSERT INTO problem

$
0
0

I'm creating a program for a friends business where he needs to store customer data.

I have 2 forms:

customerForm and addcustForm.

When he opens the customers form, and clicks the "Add New Customer" button the addcustForm opens with a series of textboxes for him to type the data into, and when he clicks save, it saves it into a dataset (displayed via datagridview on the customersForm).

Just getting a slight problem, When I click save it inserts blank text to the columns, I think its something to do with this line:

string sqlText = "INSERT INTO [customerTBL] ([First Name],[Surename],[Address Line 1],[Address Line 2],[Post Code],[County],[Email Address],[Contact Number]) VALUES('','','','','','','','')";

What do I have to enter in the 'VALUES' section for the data to be added correctly?

The whole code for addcustForm is:

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;
using System.Configuration;


namespace Lewis_Warby_Airbrushing
{
    public partial class addcustForm : Form
    {
        public addcustForm()
        {
            InitializeComponent();
        }
private void cancelBTN_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Changes have not been saved.", "",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
         this.Close();
        }

        public String strFirst = "";
        public String strSurename = "";
        public String strAddress1 = "";
        public String strAddress2 = "";
        public String strCounty = "";
        public String strPost = "";
        public String strContact = "";
        public String strEmail = "";


        //code for the save button
        private void saveBTN_Click(object sender, EventArgs e)
        {
            {
                if (String.IsNullOrEmpty(txtFirst.Text))
                {
                    MessageBox.Show("Please input First Name");
                }

                if (String.IsNullOrEmpty(txtSurename.Text))
                {
                    MessageBox.Show("Please input Surename ");
                }

                if (String.IsNullOrEmpty(txtAddress1.Text))
                {
                    MessageBox.Show("Please input Adddress Line 1");

                }
                if (String.IsNullOrEmpty(txtAddress2.Text))
                {
                    MessageBox.Show("Please input Address Line 2");
                }
                if (String.IsNullOrEmpty(txtContact.Text))
                {
                    MessageBox.Show("Please input a Contact Number");
                }
                if (String.IsNullOrEmpty(txtCounty.Text))
                {
                    MessageBox.Show("Please input County");
                }

                if (String.IsNullOrEmpty(txtEmail.Text))
                {
                    MessageBox.Show("Please input an Email Address");
                }
                if (String.IsNullOrEmpty(txtPost.Text))
                {
                MessageBox.Show("Please input Post COde");
                 }
            }

            strFirst = txtFirst.Text;
            strSurename = txtSurename.Text;
            strAddress1 = txtAddress1.Text;
            strAddress2 = txtAddress2.Text;
            strPost = txtPost.Text;
            strCounty = txtCounty.Text;
            strEmail = txtEmail.Text;
            strContact = txtContact.Text;
            string sqlText = "INSERT INTO [customerTBL] ([First Name],[Surename],[Address Line 1],[Address Line 2],[Post Code],[County],[Email Address],[Contact Number]) VALUES('','','','','','','','')";
            var connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Customers\customers.mdf;Integrated Security=True;User Instance=True";

            ExecuteNonQuery(connectionString, CommandType.Text, sqlText);
            this.Close();
        }
        int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText)
        {
            SqlCommand cmd = new SqlCommand();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = cmdText;
             int val = cmd.ExecuteNonQuery();
                return val;
            }
        }
    
        

       

    }
}

Viewing all articles
Browse latest Browse all 12583

Trending Articles