Reputation: 950
I'm trying to allow my users to sign up in a form, the table where the data will be stored once the user clicks on the "sign up" button contains 2 foreign keys that can be NULL, however when the I try to submit a new sign up form, I get the error Column Name or number of supplied values does not match table definition and I do not really know what the issue is. I have searched for a lot of solutions but none of them worked for this case. Any help is appreciated!
Here is the code:
protected void signup_btn_Click(object sender, EventArgs e)
{
string strcon = "Data Source=(LocalDB)\\MSSQLLocalDB; AttachDbFilename =C:\\Users\\user\\source\\repos\\Test\\Test\\App_Data\\database.mdf; Integrated Security = True";
SqlConnection con = new SqlConnection(strcon);
con.Open();
String command = "INSERT INTO writer VALUES (@writer_Fn, @writer_Ln, @Age, @Username, @Password, @Email)";
SqlCommand com = new SqlCommand(command, con);
com.Parameters.AddWithValue("@writer_Fn", first_box.Text);
com.Parameters.AddWithValue("@writer_Ln", last_box.Text);
com.Parameters.AddWithValue("@Age", age_box.Text);
com.Parameters.AddWithValue("@Username", user_box.Text);
com.Parameters.AddWithValue("@Password", confirm_box.Text);
com.Parameters.AddWithValue("@Email", email_box.Text);
com.ExecuteNonQuery();
}
This is the table definition of the "writer" table:
CREATE TABLE [dbo].[writer]
(
[writerID] INT IDENTITY (1, 1) NOT NULL,
[writer_Fn] CHAR (15) NOT NULL,
[writer_Ln] CHAR (15) NOT NULL,
[Age] INT NULL,
[Username] VARCHAR (25) NOT NULL,
[Password] VARCHAR (15) NOT NULL,
[Email] VARCHAR (25) NOT NULL,
[tran_FID] INT NULL,
[content_FID] INT NULL,
PRIMARY KEY CLUSTERED ([writerID] ASC),
CONSTRAINT [FK_writer_content]
FOREIGN KEY ([content_FID]) REFERENCES [dbo].[content] ([ContentID]),
CONSTRAINT [FK_writer_transaction]
FOREIGN KEY ([tran_FID]) REFERENCES [dbo].[transaction] ([tranID])
);
Upvotes: 0
Views: 67
Reputation: 33738
You aren't supplying the column names in your INSERT statement..
it should be:
INSERT
INTO writer (writer_Fn, writer_Ln, Age, Username, Password, Email)
VALUES (@writer_Fn, @writer_Ln, @Age, @Username, @Password, @Email)
Upvotes: 3