Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I get thie exception when i click the register button

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Collections.Specialized;
using System.Data;
using System.Text.RegularExpressions;
namespace Sup
{
    /// <summary>
    /// Interaction logic for Signup.xaml
    /// </summary>
    public partial class Signup : Window
    {
        public Signup()
        {
            InitializeComponent();
        }
        private void Reset()
        {
            textBoxAddress.Text = "";
            textBoxEmail.Text = "";
            textBoxFirstName.Text = "";
            textBoxLastName.Text = "";
            passwordBox1.Password = "";
            passwordBoxConfirm.Password = "";
        }
        private void registerButton_Click(object sender, RoutedEventArgs e)
        {
            if (textBoxEmail.Text.Length == 0)
            {

                MessageBox.Show("Enter an email.");

                textBoxEmail.Focus();

            }

            else if (!Regex.IsMatch(textBoxEmail.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
            {

                MessageBox.Show("Enter a valid email.");

                textBoxEmail.Select(0, textBoxEmail.Text.Length);

                textBoxEmail.Focus();

            }

            else
            {

                string firstname = textBoxFirstName.Text;

                string lastname = textBoxLastName.Text;

                string email = textBoxEmail.Text;

                string password = passwordBox1.Password;

                if (passwordBox1.Password.Length == 0)
                {

                    MessageBox.Show("Enter password.");

                    passwordBox1.Focus();

                }

                else if (passwordBoxConfirm.Password.Length == 0)
                {

                    MessageBox.Show("Enter Confirm password.");

                    passwordBoxConfirm.Focus();

                }

                else if (passwordBox1.Password != passwordBoxConfirm.Password)
                {

                    MessageBox.Show("Confirm password must be same as password.");

                    passwordBoxConfirm.Focus();

                }

                else
                {

                    string address = textBoxAddress.Text;

                    SqlConnection con = new SqlConnection("Properties.Settings.Default.usersConnectionString");
                    
                        con.Open();

                        SqlCommand cmd = new SqlCommand("Insert into Registration (FirstName,LastName,Email,Password,Address) values('" + firstname + "','" + lastname + "','" + email + "','" + password + "','" + address + "')", con);

                        cmd.CommandType = CommandType.Text;

                        cmd.ExecuteNonQuery();

                        con.Close();

                        MessageBox.Show("You have Registered successfully.");

                        Reset();
                    
                }

            }
        }
    }
}
Posted
Updated 21-Feb-14 22:56pm
v2

Start by checking your input string: It's very, very wrong!
C#
SqlConnection con = new SqlConnection("Properties.Settings.Default.usersConnectionString");

You are passing a fixed string as the connection string, when probably you want to pass the content of the setting information instead.

Try removing the double quotes:
SqlConnection con = new SqlConnection(Properties.Settings.Default.usersConnectionString);
I don't guarantee that will fix it - depends on what you have in the settings file - but it does stand a much better chance!
 
Share this answer
 
Comments
Heyrbiar 22-Feb-14 5:10am    
Thanks! this code is working but now i get this exception 'String or binary data would be truncated.The statement has been terminated.'
please help
OriginalGriff 22-Feb-14 5:17am    
You need to look at two things: the strings you are passing to SQL, and the columns you declared. At least one of the strings you are passing is too big for the column, so either you need to get your user to enter shorter data, or increase the column size.

And please, don't do it like that: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
In addition, Never store passwords in clear text - it is a major security risk. There is some information on how to do it here:
http://www.codeproject.com/Tips/186585/Password-Storage-How-to-do-it.aspx
Maarten Kools 22-Feb-14 5:18am    
+5 for all the good tips!
On this line

C#
SqlConnection con = new SqlConnection("Properties.Settings.Default.usersConnectionString");


Remove the quotes. You're passing a string "Properties.Settings.Default.usersConnectionString" instead of the value of that property.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900