Click here to Skip to main content
15,912,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

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

        private void SignButton_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=(localdb)\\Projects;Initial Catalog=MyDatabase;Integrated Security=True;");
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO User(Username,Password,Firstname,MiddleInitial,Lastname,Age,Phone,Address,City,State,Email) VALUES(@user,@pass,@first,@middle,@last,@age,@phone,@add,@city,@state,@email)",con);

            cmd.Parameters.AddWithValue("@user",UsernameTB.Text);
            cmd.Parameters.AddWithValue("@pass",PasswordTB.Text);
            cmd.Parameters.AddWithValue("@first",FirstnameTB.Text);
            cmd.Parameters.AddWithValue("@middle",MiddleTB.Text);
            cmd.Parameters.AddWithValue("@last",LastnameTB.Text);
            cmd.Parameters.AddWithValue("@age",AgeTB.Text);            
            cmd.Parameters.AddWithValue("@phone",PhoneTB.Text);
            cmd.Parameters.AddWithValue("@add",AddressTB.Text);
            cmd.Parameters.AddWithValue("@city",CityTB.Text);
            cmd.Parameters.AddWithValue("@state",StateTB.Text);
            cmd.Parameters.AddWithValue("@email",EmailTB.Text);

            cmd.ExecuteNonQuery();
            MessageBox.Show("Record has been inserted");
            con.Close();
        }
    }
}
Posted
Updated 8-Oct-14 15:42pm
v2
Comments
ChauhanAjay 8-Oct-14 21:44pm    
Try adding the try catch block in the code and check for any kind of exceptions.
Bryan Pogenio 8-Oct-14 21:49pm    
I added the try block code and it still shows the same error
ChauhanAjay 8-Oct-14 22:22pm    
can you please put your updated code here.
Bryan Pogenio 8-Oct-14 22:31pm    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

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

private void SignButton_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection("Data Source=(localdb)\\Projects;Initial Catalog=MyDatabase;Integrated Security=True;");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO User(Username,Password,Firstname,MiddleInitial,Lastname,Age,Phone,Address,City,State,Email) VALUES(@user,@pass,@first,@middle,@last,@age,@phone,@add,@city,@state,@email)",con);


cmd.Parameters.AddWithValue("@user", UsernameTB.Text);
cmd.Parameters.AddWithValue("@pass", PasswordTB.Text);
cmd.Parameters.AddWithValue("@first", FirstnameTB.Text);
cmd.Parameters.AddWithValue("@middle", MiddleTB.Text);
cmd.Parameters.AddWithValue("@last", LastnameTB.Text);
cmd.Parameters.AddWithValue("@age", AgeTB.Text);
cmd.Parameters.AddWithValue("@phone", PhoneTB.Text);
cmd.Parameters.AddWithValue("@add", AddressTB.Text);
cmd.Parameters.AddWithValue("@city", CityTB.Text);
cmd.Parameters.AddWithValue("@state", StateTB.Text);
cmd.Parameters.AddWithValue("@email", EmailTB.Text);

cmd.ExecuteNonQuery();
MessageBox.Show("Record has been inserted");
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

}
}

1 solution

Try running your INSERT command in SQL Server Management Studio with all the values you are trying to insert and see if it is throwing any error. The Exception you are receiving is most probably being thrown from SQL Server
 
Share this answer
 
Comments
Bryan Pogenio 8-Oct-14 22:30pm    
i created my database in SQL server object explorer, i dont have sql server management studio here in my laptop
RIPSolution 8-Oct-14 22:37pm    
So whats the message you are getting as the part of SQLException? Can you copy and paste the message or stack trace?
Bryan Pogenio 8-Oct-14 23:12pm    
when i press submit button it says "incorrect syntax near the keyword 'User'"
RIPSolution 8-Oct-14 23:21pm    
Well there you go. The tablename "User" is conflicting with the predefined User keyword of SQL server and hence its throwing error. Put your table name in square brackets in the Insert statement and it should work.

SqlCommand cmd = new SqlCommand("INSERT INTO [User](Username,Password,Firstname,MiddleInitial,Lastname,Age,Phone,Address,City,State,Email) VALUES(@user,@pass,@first,@middle,@last,@age,@phone,@add,@city,@state,@email)",con);
Bryan Pogenio 8-Oct-14 23:27pm    
thank you so much it works now!! you saved my life this is for my thesis I am creating an attendance system! again thank you :)

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



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