Nombre de columna no válido SQL Server

Whenever I submit my form, it gives me an error code:

"invalid column name" for all of the eventspecialist, phone, phon2, etc. and @SPECIALIST, @CUST_PHONE, @CUST_PHONE2, etc.

Here is my code below, perhaps I'm not writing the SQL statement or parameters correctly? The ones with @ infront are what's in my database, and the other lowercase ones are my textboxes. I'm relatively new to this type of coding.

Edit: Changed my INSERT INTO statement to what was suggested. The error still persists, but is only minimized to

"Invalid column name eventspecialist, Invalid column name phone, Invalid column name phone2, etc."

private void execution(string eventspecialist, string phone, string phone2, string firstname, string lastname, string besttime, string companyname, string nonprofit, string requesteddate, string requestedtime, string attendance, string eventtype, string other, string leadsource, string notes, string catering, string bar, string damagedeposit, string dancefloor, string griddate, string gridnotes, string comments)
{
    SqlConnection conn = new SqlConnection(GetConnectionString());

    string sql = "INSERT INTO tblcontacts (@SPECIALIST, @CUST_PHONE1, @CUST_PHONE2, @CUST_FNAME, @CUST_LNAME, @BEST_TIME, @COMPANY_NAME, @NONPROFIT, @REQ_DATE, @REQ_TIME, @ATTENDANCE, @EVENT_TYPE, @OTHER_DESC, @LEAD_SOURCE, @NOTES, @CATERING, @BAR, @DAMAGE_DEPOSIT, @DANCE_FLOOR) VALUES (eventspecialist, phone, phone2, firstname, lastname, besttime, companyname, nonprofit, requesteddate, requestedtime, attendance, eventtype, other, leadsource, notes, catering, bar, damagedeposit, dancefloor)";
    string sql2 = "INSERT INTO tblnotes (@NOTEDATE, @NOTEBY, @COMMENTS) VALUES (griddate, gridnotes, comments)";

    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);

        cmd.Parameters.Add("@SPECIALIST", SqlDbType.NVarChar, 50).Value = eventspecialist;
        cmd.Parameters.Add("@CUST_PHONE1", SqlDbType.NVarChar, 50).Value = phone;
        cmd.Parameters.Add("@CUST_PHONE2", SqlDbType.NVarChar, 50).Value = phone2;
        cmd.Parameters.Add("@CUST_FNAME", SqlDbType.NVarChar, 50).Value = firstname;
        cmd.Parameters.Add("@CUST_LNAME", SqlDbType.NVarChar, 50).Value = lastname;
        cmd.Parameters.Add("@BEST_TIME", SqlDbType.NVarChar, 50).Value = besttime;
        cmd.Parameters.Add("@COMPANY_NAME", SqlDbType.NVarChar, 225).Value = companyname;
        cmd.Parameters.Add("@NONPROFIT", SqlDbType.NVarChar, 10).Value = nonprofit;
        cmd.Parameters.Add("@REQ_DATE", SqlDbType.Date, 20).Value = requesteddate;
        cmd.Parameters.Add("@REQ_TIME", SqlDbType.Time, 20).Value = requestedtime;
        cmd.Parameters.Add("@ATTENDANCE", SqlDbType.Int, 50).Value = attendance;
        cmd.Parameters.Add("@EVENT_TYPE", SqlDbType.NVarChar, 50).Value = eventtype;
        cmd.Parameters.Add("@OTHER_DESC", SqlDbType.NVarChar, 225).Value = other;
        cmd.Parameters.Add("@LEAD_SOURCE", SqlDbType.NVarChar, 50).Value = leadsource;
        cmd.Parameters.Add("@NOTES", SqlDbType.NVarChar, 225).Value = notes;
        cmd.Parameters.Add("@CATERING", SqlDbType.NVarChar, 1).Value = catering;
        cmd.Parameters.Add("@BAR", SqlDbType.NVarChar, 1).Value = bar;
        cmd.Parameters.Add("@DAMAGE_DEPOSIT", SqlDbType.NVarChar, 19).Value = damagedeposit;
        cmd.Parameters.Add("@DANCE_FLOOR", SqlDbType.Money).Value = Decimal.Parse(dancefloor);


        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException ex_msg)
    {
        string msg = "Error occured while inserting";
        msg += ex_msg.Message;
        throw new Exception(msg);
    }
    finally
    {

        conn.Close();
    }
    try
    {
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql2, conn);

        cmd.Parameters.Add("@NOTEDATE", SqlDbType.Date, 50).Value = griddate;
        cmd.Parameters.Add("@NOTEBY", SqlDbType.NVarChar, 50).Value = gridnotes;
        cmd.Parameters.Add("@COMMENTS", SqlDbType.NVarChar, 50).Value = comments;

        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }
    catch (System.Data.SqlClient.SqlException ex_msg)
    {
        string msg = "Error occured while inserting";
        msg += ex_msg.Message;
        throw new Exception(msg);
    }
    finally
    {

        conn.Close();
    }

}
protected void Page_Load(object sender, EventArgs e)
{
    griddate.Text = DateTime.Now.ToString("yyyy/MM/dd");
}
protected void submit_Click(object sender, EventArgs e)
{
    if (requesteddate.Text == "")
    {
        Finish.Text = "Please complete the form!";
    }
    else if (requestedtime.Text == "")
    {
        Finish.Text = "Please complete the form!";
    }
    else if (attendance.Text == "")
    {
        Finish.Text = "Please complete the form!";
    }
    else
    {
        execution(eventspecialist.Text, phone.Text, phone2.Text, firstname.Text, lastname.Text, besttime.SelectedItem.Text, companyname.Text, nonprofit.Text, requesteddate.Text, requestedtime.Text, attendance.Text, eventtype.SelectedItem.Text, other.Text, leadsource.SelectedItem.Text, notes.Text, catering.Text, bar.Text, damagedeposit.Text, dancefloor.SelectedItem.Text, griddate.Text, gridnotes.SelectedItem.Text, comments.Text);
        Finish.Visible = false;
        conform.Visible = true;
    }
}

preguntado el 09 de marzo de 12 a las 13:03

You have inverted the column names with the parameter list -

Even after doing that, I still get the error "Invalid column name eventspecialist, Invalid column name phone, Invalid column name phone2, etc.". Any ideas? -

What are the real names of your columns in the database? -

What's written above, just without the @. -

As I expected, so look at my answer. -

7 Respuestas

First column names and then variables

string sql = "INSERT INTO tblcontacts (Col1,Col2,...) VALUES (@Var1,@Var2,...)"

respondido 09 mar '12, 14:03

Even after doing that, I still get the error "Invalid column name eventspecialist, Invalid column name phone, Invalid column name phone2, etc.". Any ideas? - Control

I suppose that your columns are

SPECIALIST, CUST_PHONE1, CUST_PHONE2, CUST_FNAME, CUST_LNAME, BEST_TIME, 
COMPANY_NAME, NONPROFIT, REQ_DATE, REQ_TIME, ATTENDANCE, EVENT_TYPE, OTHER_DESC, 
LEAD_SOURCE, NOTES, CATERING, BAR, DAMAGE_DEPOSIT, DANCE_FLOOR

if so you should change your INSERT INTO in this way

INSERT INTO tblcontacts 
(SPECIALIST, CUST_PHONE1, CUST_PHONE2, CUST_FNAME, CUST_LNAME, BEST_TIME, 
COMPANY_NAME, NONPROFIT, REQ_DATE, REQ_TIME, ATTENDANCE, EVENT_TYPE, OTHER_DESC, LEAD_SOURCE, 
NOTES, CATERING, BAR, DAMAGE_DEPOSIT, DANCE_FLOOR)
VALUES
(@SPECIALIST, @CUST_PHONE1, @CUST_PHONE2, @CUST_FNAME, @CUST_LNAME, @BEST_TIME, @COMPANY_NAME, 
 @NONPROFIT, @REQ_DATE, @REQ_TIME, @ATTENDANCE, @EVENT_TYPE, @OTHER_DESC, @LEAD_SOURCE, @NOTES, 
 @CATERING, @BAR, @DAMAGE_DEPOSIT, @DANCE_FLOOR)

En su execution method you are creating parameters, but, then, in the insert statement
first put the real column names and then, in the value part, the parameters names
(with @prefix)

As a side note, next time, try to format better your code.

respondido 09 mar '12, 14:03

What do you mean try to format better your code? I know it's probably got a lot of inefficiencies, but getting this to simply just work is a step that I want to accomplish first. Once again, I appreciate the explanation. :) - Control

Ah man you beat me to it while I was trying to get the insert code to work....... - Zo tiene

No, I don't talk about efficiency here. When you put on SO lines of code so long you should format with carriage returns. I think that's why many have not understood your problem - Steve

Alright. Once again though, I'm pretty new to this, so I was just trying to take it step by step and see how much I could accomplish and get done. I'd like to definitely further my knowledge of programming, and if this is something important as I'm sure it is, I'll start looking into it and learning it. Do you have any helpful links you might suggest? If not, Google has always been a good friend of mine in coding-hell. - Control

Your columns and values are the wrong way round:

string sql = "INSERT INTO tblcontacts (eventspecialist, phone, phone2, firstname, lastname, besttime, companyname, nonprofit, requesteddate, requestedtime, attendance, eventtype, other, leadsource, notes, catering, bar, damagedeposit, dancefloor) VALUES (@SPECIALIST, @CUST_PHONE1, @CUST_PHONE2, @CUST_FNAME, @CUST_LNAME, @BEST_TIME, @COMPANY_NAME, @NONPROFIT, @REQ_DATE, @REQ_TIME, @ATTENDANCE, @EVENT_TYPE, @OTHER_DESC, @LEAD_SOURCE, @NOTES, @CATERING, @BAR, @DAMAGE_DEPOSIT, @DANCE_FLOOR)";

respondido 09 mar '12, 14:03

You have the order backwards: generalised, it is:

insert into tableName (column1, ..., column20) values (value1, ..., value20)

so; just swap the columns and values:

string sql = "INSERT INTO tblcontacts (eventspecialist, phone, phone2, firstname, lastname, besttime, companyname, nonprofit, requesteddate, requestedtime, attendance, eventtype, other, leadsource, notes, catering, bar, damagedeposit, dancefloor) VALUES (@SPECIALIST, @CUST_PHONE1, @CUST_PHONE2, @CUST_FNAME, @CUST_LNAME, @BEST_TIME, @COMPANY_NAME, @NONPROFIT, @REQ_DATE, @REQ_TIME, @ATTENDANCE, @EVENT_TYPE, @OTHER_DESC, @LEAD_SOURCE, @NOTES, @CATERING, @BAR, @DAMAGE_DEPOSIT, @DANCE_FLOOR)";
string sql2 = "INSERT INTO tblnotes (griddate, gridnotes, comments) VALUES (@NOTEDATE, @NOTEBY, @COMMENTS)";

respondido 09 mar '12, 14:03

You got your INSERT statements all wrong.....

En lugar de esto:

INSERT INTO tblcontacts (@SPECIALIST, @CUST_PHONE1, .....)
VALUES (eventspecialist, phone, .....);

You need to use the opposite:

INSERT INTO tblcontacts (eventspecialist, phone, .....)
VALUES (@SPECIALIST, @CUST_PHONE1, .....);

Necesitas tener el table's column names in the list after the table name, and the parámetros (con un líder @) después de la VALUES

respondido 09 mar '12, 14:03

You have entered your paramters as columns and your columns as parameters. Try the other way around.

respondido 09 mar '12, 14:03

Replace your code with the following

"INSERT INTO tblcontacts (SPECIALIST, CUST_PHONE1, CUST_PHONE2, CUST_FNAME, CUST_LNAME, BEST_TIME, COMPANY_NAME, NONPROFIT, REQ_DATE, REQ_TIME, ATTENDANCE, EVENT_TYPE, OTHER_DESC, LEAD_SOURCE, NOTES, CATERING, BAR, DAMAGE_DEPOSIT, DANCE_FLOOR) VALUES (@eventspecialist, @phone, @phone2, @firstname, @lastname, @besttime, @companyname, @nonprofit, @requesteddate, @requestedtime, @attendance, @eventtype, @other, @leadsource, @notes, @catering, @bar, @damagedeposit, @dancefloor)";

y aquí

    cmd.Parameters.Add("@eventspecialist", SqlDbType.NVarChar, 50).Value = eventspecialist;
    cmd.Parameters.Add("@phone", SqlDbType.NVarChar, 50).Value = phone;
    cmd.Parameters.Add("@phone2", SqlDbType.NVarChar, 50).Value = phone2;
    cmd.Parameters.Add("@firstname", SqlDbType.NVarChar, 50).Value = firstname;
    cmd.Parameters.Add("@lastname", SqlDbType.NVarChar, 50).Value = lastname;
    cmd.Parameters.Add("@besttime", SqlDbType.NVarChar, 50).Value = besttime;
    cmd.Parameters.Add("@companyname", SqlDbType.NVarChar, 225).Value = companyname;
    cmd.Parameters.Add("@nonprofit", SqlDbType.NVarChar, 10).Value = nonprofit;
    cmd.Parameters.Add("@requesteddate", SqlDbType.Date, 20).Value = requesteddate;
    cmd.Parameters.Add("@requestedtime", SqlDbType.Time, 20).Value = requestedtime;
    cmd.Parameters.Add("@attendance", SqlDbType.Int, 50).Value = attendance;
    cmd.Parameters.Add("@eventtype", SqlDbType.NVarChar, 50).Value = eventtype;
    cmd.Parameters.Add("@other", SqlDbType.NVarChar, 225).Value = other;
    cmd.Parameters.Add("@leadsource", SqlDbType.NVarChar, 50).Value = leadsource;
    cmd.Parameters.Add("@notes", SqlDbType.NVarChar, 225).Value = notes;
    cmd.Parameters.Add("@catering", SqlDbType.NVarChar, 1).Value = catering;
    cmd.Parameters.Add("@bar", SqlDbType.NVarChar, 1).Value = bar;
    cmd.Parameters.Add("@damagedeposit", SqlDbType.NVarChar, 19).Value = damagedeposit;
    cmd.Parameters.Add("@dancefloor", SqlDbType.Money).Value = Decimal.Parse(dancefloor);

respondido 09 mar '12, 14:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.