1

I'm having problem saving data to a local database on my program. It's doesn't store the value's inputted by the user in the User table. What could be the problem, some code and picture below.


public void addInformationToDatabase()
    {
        string Sex = ddlGender.Text;
        string Name = tbxName.Text;
        string DOB = tbxDOB.Text;

        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection Con = new SqlConnection(connectionString);

        SqlCommand command = new SqlCommand();
        command.Connection = Con;
        command.CommandType = CommandType.Text;
        command.CommandText = "INSERT INTO User (Gender,Name,DOB) VALUES(@Sex,@Name,@DOB)";

        command.Parameters.AddWithValue("@Sex", Sex);
        command.Parameters.AddWithValue("@FirstName", Name);
        command.Parameters.AddWithValue("@DOB", DOB);

        try
        {
            Con.Open();
            command.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        finally
        {
            Con.Close();
        }
    }

protected void btnNext_Click(object sender, EventArgs e)
    {
        addInformationToDatabase();
    }

ERROR

Were I want to store user information

User table

Any help much appreciated

0

1 Answer 1

3

USER is a reserved keyword for Sql Server. To use it you need to enclose it in square brackets

 command.CommandText = "INSERT INTO [User] (Gender,Name,DOB) VALUES(@Sex,@Name,@DOB)";

If you are still able to change that name, I suggest to do it as soon as possible, because you will have to remember this problem every time you work with that table

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

3 Comments

Awh, my bad. Thanks for helping me out!
Yeah, sorry, just realised. Changed it now and it's working, thanks for the help!
DOB is a date in the table, you create a parameter with AddWithValue passing a string. So, the parameter type is an NVarWChar not a Date. You could resolve with AddWithValue("@DOB", Convert.ToDateTime(DOB))

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.