Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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;
using System.Data.SqlClient;
using System.Configuration;

public partial class Registration : System.Web.UI.Page
{
    SqlDataAdapter da;
    DataSet ds;

    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["shop02ConnectionString"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        
    }
    
    protected void Button1_Click1(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("insert into shop02 ( firstname, lastname, gender,email,city, state,country, mobile) values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox3.Text+"', '"+TextBox4.Text+"','"+TextBox5.Text+"', '"+TextBox6.Text+"', '"+TextBox7.Text+"', '"+TextBox8.Text+"')", con);
        da.SelectCommand.ExecuteNonQuery();


    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("select count(firstname) from shop02  where firstname = '" + TextBox1.Text + "'", con);
        ds = new DataSet();
        da.Fill(ds, "regis");
        if (ds.Tables["regis"].Rows.Count == 1 && ds.Tables["regis"].Rows[0][0] == "1")
        {
            Label9.Text = "firstname not ava";
            Label9.BackColor = System.Drawing.Color.Azure;
        }

     }
}



and table is..
 id int identity(1,1) primary key,
  firstname varchar(88),
  lastname varchar(88),
  gender varchar(88),
  email varchar(88),
  city varchar(88),
  state varchar(88),
  country varchar(88),
  mobile varchar(88)
Posted
Updated 26-Dec-17 0:45am
v2
Comments
Thanks7872 1-Apr-15 3:39am    
What is the question?
Member 10874581 1-Apr-15 3:43am    
when i am submiting data to database the error is coming and error is...There are fewer columns in the INSERT statement than values specified in the VALUES clause
Sascha Lefèvre 1-Apr-15 3:45am    
Isn't that rather self-explanatory?

C#
da = new SqlDataAdapter("insert into shop02 ( firstname, lastname, gender,email,city, state,country, mobile) values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox3.Text+"', '"+TextBox4.Text+"','"+TextBox5.Text+"', '"+TextBox6.Text+"', '"+TextBox7.Text+"', '"+TextBox8.Text+"')", con);

There are 8 column names in the first bracket pair and 9 values in the following VALUES-clause. Either add one column or remove one value.

Please take also a look at one of my past answers regarding database-access:
how to loop sql server table to create a datagridview - sql table field matches csv field[^]
Because there's a lot you could do better. First and foremost using SQL-Parameters which probably would have allowed you to spot the error yourself because when using SQL-Parameters you can actually read your SQL-Statements.
 
Share this answer
 
Well yes...
C#
da = new SqlDataAdapter("insert into shop02 ( firstname, lastname, gender,email,city, state,country, mobile) values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox3.Text+"', '"+TextBox4.Text+"','"+TextBox5.Text+"', '"+TextBox6.Text+"', '"+TextBox7.Text+"', '"+TextBox8.Text+"')", con);
You specify 8 columns, and provide 9 values. Where should it put the "extra" one? Why are you including TextBox3 twice?

And don't do it like that: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it is three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
Comments
Member 10874581 1-Apr-15 3:58am    
thanks a lot sir..
OriginalGriff 1-Apr-15 4:08am    
You're welcome!
Member 10874581 1-Apr-15 4:16am    
sir can i have your mail id
OriginalGriff 1-Apr-15 4:23am    
Sorry, no - we don't do "private solutions" here:
http://www.codeproject.com/Messages/5031627/Live-chat-with-an-User.aspx
Member 10874581 1-Apr-15 4:38am    
ok mention not
You have an extra
TextBox3.Text
Observe:

8 columns in your column list:

"insert into shop02 ( 
firstname
lastname
gender
email
city
state
country
mobile) 


9 in your values...

 values(
TextBox1.Text

TextBox2.Text

TextBox3.Text

TextBox3.Text
 
TextBox4.Text

TextBox5.Text
 
TextBox6.Text
 
TextBox7.Text
 
TextBox8.Text
 
Share this answer
 
Comments
Dave Kreskowiak 24-Dec-17 13:38pm    
Asked and ANSWERED almost 3 years ago.
[no name] 24-Dec-17 17:49pm    
Oops! I saw it on the sidebar. I guess I don't know how to tell if a question has been answered, but why did you down vote it? My answer was the most correct and precise.
CHill60 27-Dec-17 13:15pm    
The number of solutions appears next to the past in the list. Alternatively, they appear below the post when you are viewing it.
You cannot tell who downvotes
[no name] 27-Dec-17 15:08pm    
I see the solutions, but I can't tell which one is the accepted solution. On some sites, it's the first one. On some, it's the one with the most votes. On this one, I've ruled out those and so I'm guessing it's the one with the green banner across the top. Regardless, people who look at this later will assume that my answer was incorrect when in fact, it was the best answer. I'll have to keep that in mind as I look for solutions to my problems moving forward. I don't care about down votes. I'm here to learn.

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