Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace WebApplication2
{
    public partial class WebForm7 : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            string pwd = args.Value;
            int l;
            l = pwd.Length;
            if (l >= 6 && l <= 8)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
             string name, gender, address, username, mobileno, email, password, cnfrmpassword;
            name = txtname.Text;
            address = txtaddr.Text;
            mobileno = txtmob.Text;
            username = TextBox4.Text;
            email = txtemail.Text;
            password = txtpass.Text;
            cnfrmpassword = txtconfirmpass.Text;
            gender = DropDownList2.SelectedItem.Text;
            SqlConnection con = new SqlConnection("Data Source=monikabishnoi\\sqlexpress;Initial Catalog=Signup;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter();
            da.InsertCommand = new SqlCommand("INSERT INTO Signup VALUES (@Name,@Gender,@Address,@Mobileno,@Username,@Email,@Password,@Cnfrmpassword");
            da.InsertCommand.Parameters.Add("@name", SqlDbType.NVarChar).Value = txtname.Text;
            da.InsertCommand.Parameters.Add("@gender", SqlDbType.NVarChar).Value = DropDownList2.Text;
            da.InsertCommand.Parameters.Add("@address", SqlDbType.NVarChar).Value = txtaddr.Text;
            da.InsertCommand.Parameters.Add("@mobileno", SqlDbType.NVarChar).Value = txtmob.Text;
            da.InsertCommand.Parameters.Add("@username", SqlDbType.NVarChar).Value= TextBox4.Text;
            da.InsertCommand.Parameters.Add("@email", SqlDbType.NVarChar).Value = txtemail.Text;
            da.InsertCommand.Parameters.Add("@password", SqlDbType.NVarChar).Value = txtpass.Text;
            da.InsertCommand.Parameters.Add("@cnfrmpassword", SqlDbType.NVarChar).Value = txtconfirmpass.Text;
            con.Open();
            da.InsertCommand.ExecuteNonQuery();
            con.Close();

        }
    }
}


in sqlserver, database entries are
name nvarchar(50)
gender nvarchar(50)
address nvarchar(50)
mobileno nvarchar(50)
username varchar(50)
email nvarchar(50)
password varchar(50)
cnfrmpassword varchar(50)


please help me in coding ..why values are not going in database after entering alll values in signup form
Posted
v2
Comments
Check line by line while debugging the code. You will know the problem.
There may be some exceptions. It will provide you a hint.
Member 10275283 15-Sep-13 10:24am    
there is no error..but after executing ,when i filll all the details in the form ,it sends me to home page,but entries don't go in the database...
You should have a Try Catch block to catch the exception. Please do that and check what is the exception.
Are you sure that your ConnectionString is correct? Because in the string, there is no username and passwords specified. Are you using windows authentication?

1 solution

Probably, it's becasue you are not listign the columns you want to enter the data to in your INSERT statement:
SQL
INSERT INTO MyTable VALUES ('hello', 'world')
will fail if there are three columns in you table, and the first one is (for example) an Identity column providing an ID value for the row. The reason why is simple: when you do not specify columns, SQL inserts values into columns by index, starting at the first column in the table, and then the second and so on.
So the INSERT above tries to insert a string 'Hello' into an integer column, and the insert fails as a result.

To prevent this, always list your column names:
SQL
INSERT INTO MyTable (UserName, Gender...) VALUES (@name, @Gender, ...)
 
Share this answer
 
Comments
Member 10275283 15-Sep-13 10:45am    
still getting the same problem..
OriginalGriff 15-Sep-13 10:50am    
The first thing you need to do is find out what the error message is then: you say there isn't one, but you also say it goes to the home page and since there is no code in your method to do a redirect, it is likely to be the SQL exception that causes that.
So start by adding a try...catch block round the whole click event handler, and show the error message on your page in the catch block. That should help find out what is going on.
Member 10275283 15-Sep-13 11:00am    
don't know how to use try n catch block :(
OriginalGriff 15-Sep-13 11:36am    
What do they teach you these days...:laugh:

try
{
...your code...
}
catch (Exception ex)
{
string showThisOnYourPage = ex.ToString();
...
}

There are those who say you should only catch the exceptions you want, and they are right, but in this case we don't have any idea what exception it is, so we need to catch the generic Exception object as sort it out later.
You can write the string to a label on your page, and write "Success!" to it as the last line in the "try" part to show if it worked. It might be worth adding it to a error file at the same time, just so you have the details easy to copy and paste later.

What happens is that if an error occurs in the code in the "try" block, execution goes immediately to the "catch" block instead of stopping the page operation. Hopefully, this means we will get the error...
Member 10275283 15-Sep-13 12:38pm    
Problem solved :) Thankyou..Now i'll be having problem in login table...:P :P

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