0

Whenever I call this function to run this query....

  public static void AddUsernameAndPassword()
    {


        SqlConnection connection = MedicalDB.EstConnection();
        connection.Open();

        string alterStatement = "ALTER TABLE PATIENT_TBL ADD USER_NAME VARCHAR(50) NULL," + 
                                " PASSWORD VARCHAR(50) NULL";

        SqlCommand alterCommand =
            new SqlCommand(alterStatement, connection);
        alterCommand.ExecuteNonQuery();
        connection.Close();
    }

through here

 private void loginButton_Click(object sender, EventArgs e)
    {
        PatientDB.AddUsernameAndPassword();
        //getPatient(1);

        passTextBox.Text = patient.userName;


    }

It tells me

Invalid column name 'USER_NAME'.

The properties are set accordingly. Please advise.

2 Answers 2

1

USER_NAME is a function in SQL Server. So if you want to use it as a column name add square brackets to it.

Something like this, [USER_NAME]

Code

string alterStatement = "ALTER TABLE PATIENT_TBL ADD [USER_NAME] VARCHAR(50) NULL," + 
                            " [PASSWORD] VARCHAR(50) NULL";

And one more thing, never save password as plain text. Use proper encryptions.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Those columns are being added to a database already filled with information. Would that make a difference? As a testing measure to make sure the column is there, I'm just expecting it to return null when querying it.
0

Your SQL command seems correct. So, check whether your connection is established. Also, I think it has already created a column in that name.

But, why to add a new column to the DB everytime you click on the button. You could first initialize the columns in the constructor like an Init(), and then you could insert values to it every time you click on the button.

1 Comment

good question...that button will be totally reassigned for the final. That was just temporary to add columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.