Invalid column name sql error

后端 未结 11 1187
梦毁少年i
梦毁少年i 2020-11-30 03:16

I am trying to enter data into my database, but it is giving me the following error:

Invalid column name

Here\'s my code

<
相关标签:
11条回答
  • 2020-11-30 04:09

    You probably need quotes around those string fields, but, you should be using parameterized queries!

    cmd.CommandText = "INSERT INTO Data ([Name],PhoneNo,Address) VALUES (@name, @phone, @address)";
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("@name", txtName.Text);
    cmd.Parameters.AddWithValue("@phone", txtPhone.Text);
    cmd.Parameters.AddWithValue("@address", txtAddress.Text);
    cmd.Connection = connection;
    

    Incidentally, your original query could have been fixed like this (note the single quotes):

    "VALUES ('" + txtName.Text + "','" + txtPhone.Text + "','" + txtAddress.Text + "');";
    

    but this would have made it vulnerable to SQL Injection attacks since a user could type in

    '; drop table users; -- 
    

    into one of your textboxes. Or, more mundanely, poor Daniel O'Reilly would break your query every time.

    0 讨论(0)
  • 2020-11-30 04:16
    con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Yna Maningding-Dula\Documents\Visual Studio 2010\Projects\LuxuryHotel\LuxuryHotel\ClientsRecords.mdf;Integrated Security=True;User Instance=True");
            con.Open();
            cmd = new SqlCommand("INSERT INTO ClientData ([Last Name], [First Name], [Middle Name], Address, [Email Address], [Contact Number], Nationality, [Arrival Date], [Check-out Date], [Room Type], [Daily Rate], [No of Guests], [No of Rooms]) VALUES (@[Last Name], @[First Name], @[Middle Name], @Address, @[Email Address], @[Contact Number], @Nationality, @[Arrival Date], @[Check-out Date], @[Room Type], @[Daily Rate], @[No of Guests], @[No of Rooms]", con);
            cmd.Parameters.Add("@[Last Name]", txtLName.Text);
            cmd.Parameters.Add("@[First Name]", txtFName.Text);
            cmd.Parameters.Add("@[Middle Name]", txtMName.Text);
            cmd.Parameters.Add("@Address", txtAdd.Text);
            cmd.Parameters.Add("@[Email Address]", txtEmail.Text);
            cmd.Parameters.Add("@[Contact Number]", txtNumber.Text);
            cmd.Parameters.Add("@Nationality", txtNational.Text);
            cmd.Parameters.Add("@[Arrival Date]", txtArrive.Text);
            cmd.Parameters.Add("@[Check-out Date]", txtOut.Text);
            cmd.Parameters.Add("@[Room Type]", txtType.Text);
            cmd.Parameters.Add("@[Daily Rate]", txtRate.Text);
            cmd.Parameters.Add("@[No of Guests]", txtGuest.Text);
            cmd.Parameters.Add("@[No of Rooms]", txtRoom.Text);
            cmd.ExecuteNonQuery();
    
    0 讨论(0)
  • 2020-11-30 04:18

    Change this line:

    cmd.CommandText = "INSERT INTO Data (Name,PhoneNo,Address) VALUES (" + txtName.Text + "," + txtPhone.Text + "," + txtAddress.Text + ");";
    

    to this:

    cmd.CommandText = "INSERT INTO Data (Name,PhoneNo,Address) VALUES ('" + txtName.Text + "','" + txtPhone.Text + "','" + txtAddress.Text + "');";
    

    Your insert command is expecting text, and you need single quotes (') between the actual value so SQL can understand it as text.

    EDIT: For those of you who aren't happy with this answer, I would like to point out that there is an issue with this code in regards to SQL Injection. When I answered this question I only considered the question in point which was the missing single-quote on his code and I pointed out how to fix it. A much better answer has been posted by Adam (and I voted for it), where he explains the issues with injection and shows a way to prevent. Now relax and be happy guys.

    0 讨论(0)
  • 2020-11-30 04:19

    Your issue seems to be the Name keyword. Rather use FullName or firstName and lastName, always try and remember to use CamelCase too.

    0 讨论(0)
  • 2020-11-30 04:20

    first create database name "School" than create table "students" with following columns 1. id 2. name 3. address

    now open visual studio and create connection:

    namespace school
    {
        public partial class Form1 : Form
        {
            SqlConnection scon;
    
    
            public Form1()
            {
    
                InitializeComponent();
    
                scon = new SqlConnection("Data Source = ABC-PC; trusted_connection = yes; Database = school; connection timeout = 30");
            }
    
    //create command
    
    SqlCommand scom = new SqlCommand("insert into students (id,name,address) values(@id,@name,@address)", scon);
    
    //pass parameters
    
    scom.Parameters.Add("id", SqlDbType.Int);
    scom.Parameters["id"].Value = textBox1.Text;
    
               scom.Parameters.Add("name", SqlDbType.VarChar);
                scom.Parameters["name"].Value = this.textBox2.Text;
    
                scom.Parameters.Add("address", SqlDbType.VarChar);
                scom.Parameters["address"].Value = this.textBox6.Text;
    
    
                scon.Open();
                scom.ExecuteNonQuery();
                scon.Close();
                reset();
    
            }

    also check solution here: http://solutions.musanitech.com/?p=6

    0 讨论(0)
提交回复
热议问题