I am currently working a registration project and 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.Sql;
using System.Data.SqlClient;

namespace Thesis
{
    public partial class Registration : Form
    {
        Form formParent = null;
        public Registration(Form par)
        {
            formParent = par;
            InitializeComponent();
        }

        private void Registration_FormClosed(object sender, FormClosedEventArgs e)
        {
            formParent.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Granodiorit\Documents\Visual Studio 2008\Projects\Thesis\Thesis\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            try
            {
                cn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }

            string username = UserName.Text;
            string password = Password.Text;
            string nickname = Nick.Text;
            string birthday = Birthday.Text;
            string ymail = Ymail.Text;
            string gmail = Gmail.Text;
            string hotmail = Hotmail.Text;
            string yaddress = YAddress.Text;
            string gaddress = GAddress.Text;
            string hotaddress = HotAddress.Text;

            string sqlquery;

            sqlquery = "INSERT INTO [User] (Username, Password, Nick, Birthday, Ymail, Yaddress, Gmail, Gaddress, Hotmail, Hotaddress) VALUES (@UserName, @Password, @Nick, @Birthday, @YMail, @YAddress, @GMail, @GAddress, @Hotmail, @HotAddress)";
            SqlCommand command = new SqlCommand(sqlquery, cn);
            command.Parameters.AddWithValue("@UserName", username);
            command.Parameters.AddWithValue("@Password", password);
            command.Parameters.AddWithValue("@Nick", nickname);
            command.Parameters.AddWithValue("@Birthday", birthday);
            command.Parameters.AddWithValue("@YMail", ymail);
            command.Parameters.AddWithValue("@YAddress", yaddress);
            command.Parameters.AddWithValue("@GMail", gmail);
            command.Parameters.AddWithValue("@GAddress", gaddress);
            command.Parameters.AddWithValue("@Hotmail", hotmail);
            command.Parameters.AddWithValue("@HotAddress", hotaddress);
            command.ExecuteNonQuery();
            this.Close();
            formParent.Show();
        }



        internal void show()
        {
            throw new NotImplementedException();
        }

        private void Registration_Load(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Granodiorit\Documents\Visual Studio 2008\Projects\Thesis\Thesis\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            try
            {
                cn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }
        }

    }
}

This code works for writing data into SQL.. Now.. My Questions are..
1. How to make a prevention for users to have the same data? (Data redundancy prevention).. The think I would like to have is When the new user put the same data, there will be a warning.. The important key will be the USERNAME and EMAIL ADDRESSES..
2. Make a notification to let the new users knew where is the message.. like a MessageBox..

Thanks in advance.. I am really noob in this.. :)

Recommended Answers

All 4 Replies

Use SELECT with WHERE clause.

SqlCommand command = new SqlCommand("select username from [user] where username=@Username" , cn);
 command.Parameters.AddWithValue("@UserName", username);

 object result=command.ExecuteScalar();
 if(result!=null) // found
   {
      //Given username is found 
      return;
    }
 ....

Can you help me how to implement it to 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.Sql;
using System.Data.SqlClient;

namespace Thesis
{
    public partial class Registration : Form
    {
        Form formParent = null;
        public Registration(Form par)
        {
            formParent = par;
            InitializeComponent();
        }

        private void Registration_FormClosed(object sender, FormClosedEventArgs e)
        {
            formParent.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Granodiorit\Documents\Visual Studio 2008\Projects\Thesis\Thesis\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            try
            {
                cn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }

            string username = UserName.Text;
            string password = Password.Text;
            string nickname = Nick.Text;
            string birthday = Birthday.Text;
            string ymail = Ymail.Text;
            string gmail = Gmail.Text;
            string hotmail = Hotmail.Text;
            string yaddress = YAddress.Text;
            string gaddress = GAddress.Text;
            string hotaddress = HotAddress.Text;

            /* -------------------- */

 SqlCommand command = new SqlCommand("select username from [user] where username=@Username" , cn);
 command.Parameters.AddWithValue("@UserName", username);

 object result=command.ExecuteScalar();
 if(result!=null) // found
   {
       MessageBox.Show("Duplicate username!!!");
      return;
    }

            /* -------------------- */

            string sqlquery;

            sqlquery = "INSERT INTO [User] (Username, Password, Nick, Birthday, Ymail, Yaddress, Gmail, Gaddress, Hotmail, Hotaddress) VALUES (@UserName, @Password, @Nick, @Birthday, @YMail, @YAddress, @GMail, @GAddress, @Hotmail, @HotAddress)";
            command = new SqlCommand(sqlquery, cn);
            command.Parameters.AddWithValue("@UserName", username);
            command.Parameters.AddWithValue("@Password", password);
            command.Parameters.AddWithValue("@Nick", nickname);
            command.Parameters.AddWithValue("@Birthday", birthday);
            command.Parameters.AddWithValue("@YMail", ymail);
            command.Parameters.AddWithValue("@YAddress", yaddress);
            command.Parameters.AddWithValue("@GMail", gmail);
            command.Parameters.AddWithValue("@GAddress", gaddress);
            command.Parameters.AddWithValue("@Hotmail", hotmail);
            command.Parameters.AddWithValue("@HotAddress", hotaddress);
            command.ExecuteNonQuery();
            this.Close();
            formParent.Show();
        }



        internal void show()
        {
            throw new NotImplementedException();
        }

        private void Registration_Load(object sender, EventArgs e)
        {
            string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Granodiorit\Documents\Visual Studio 2008\Projects\Thesis\Thesis\Database1.mdf;Integrated Security=True;User Instance=True";
            SqlConnection cn = new SqlConnection(connection);
            try
            {
                cn.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Did not connect");
            }
        }

    }
}

Thanks.. appreciate it so much.. :)

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.