Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm writing following code to insert data in SQL database code is working properly but no data is going in the database.Please help me with this. I'll really appreciate. My code is as follows:

conn.Close();
conn.Open();
string insertstring = "INSERT INTO dbo.login([username],[password])VALUES(@username,@password)";
SqlCommand cmd1 = new SqlCommand(insertstring, conn);
cmd1.Parameters.AddWithValue("@username", username.Text);
cmd1.Parameters.AddWithValue("@password", password.Text);
cmd1.ExecuteNonQuery();

conn.Close();

Response.Redirect("confirm.aspx");



It is a code for signup page And on confirm.aspx page I've Continue button which again takes us to login page. When I create a new account its accepting the new user but there is no data in my database
Posted
Updated 29-Apr-10 11:45am
v2

Obviously this is unacceptable code on a professional level, but if you're just playing around, it's a fine starting place. I believe executenonquery returns a number to tell you how many rows were changed by the query. Grab it and see if it's 0. Put a breakpoint before the redirect, and check your database at that point. If the data is there, then try to work out why your database gets reset.

And, do some reading up on how to properly structure a database driven project. This does look like code someone will use, I hope no-one is paying for it, but you may as well learn to make your own code maintainable also
 
Share this answer
 
Pay attention to what Christian told you. Your code is only good enough for playing around, and is not production quality code.

Beyond that, I don't see your connection string being set. You normally pass it as a parameter when you create your SqlConnection object. Also, if you put a try/catch around that block of code, you'll probably be able to tell what's wrong.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900