Click here to Skip to main content
15,075,525 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i m geting a error
Column name or number of supplied values does not match table definition



mycolumn name all are proper data is saving in databse but also showing a error

Column name or number of supplied values does not match table definition
C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
        SqlCommand cmd = new SqlCommand();

        protected void Button1_Click(object sender, EventArgs e)
        {

            try
            {
                cmd.Connection = con; //assigning connection to command
                cmd.CommandType = CommandType.Text; //representing type of command

                cmd.CommandText = "insert into User_Register values(@Fname,@Lname,@UserID,@Password,@MobileNo,@Email,@ProfileImage)";

                //adding parameters with value
                cmd.Parameters.AddWithValue("@Fname", name.Text.ToString());
                cmd.Parameters.AddWithValue("@Lname", lname.Text.ToString());
                cmd.Parameters.AddWithValue("@UserID", userid.Text.ToString());
                cmd.Parameters.AddWithValue("@Password", password.Text.ToString());

                cmd.Parameters.AddWithValue("@MobileNo", cellno.Text.ToString());
                cmd.Parameters.AddWithValue("@Email", emailid.Text.ToString());

                int img = FileUpload1.PostedFile.ContentLength;

                byte[] msdata = new byte[img];

                FileUpload1.PostedFile.InputStream.Read(msdata, 0, img);

               cmd.Parameters.AddWithValue("@ProfileImage", msdata);
                con.Open(); //opening connection


                cmd.ExecuteNonQuery();  //executing query
                cmd.CommandText = "insert into login values('" + userid.Text + "','" + password.Text + "')";
                cmd.ExecuteNonQuery();
                Session["username"] = userid.Text;
                Session["password"] = password.Text;
             
                FormsAuthentication.RedirectFromLoginPage(userid.Text, true);
                con.Close(); //closing connection
               // ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Login Successfully...')</script>");
               // Label1.Text = "Registered Successfully..";
                Response.Redirect("default.aspx");

            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                Label1.Text = ex.Message.ToString();
                
            }
        }





XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="register.aspx.cs" Inherits="torqbolt.register" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

  <meta charset="UTF-8">

  <title>Torq Bolt Registration</title>

    <style>
@import url(http://fonts.googleapis.com/css?family=Exo:100,200,400);
@import url(http://fonts.googleapis.com/css?family=Source+Sans+Pro:700,400,300);

body{
    margin: 0;
    padding: 0;
    background: #fff;

    color: #fff;
    font-family: Arial;
    font-size: 12px;
}

</style>

    <script src="js/prefixfree.min.js"></script>

</head>

<body>
    <form id="form1" runat="server">
<div class="body"></div>
<div class="grad"></div>
        <div class="header">
            <div>Site<span>Register</span></div>
        </div>
        <br>
        <div class="login">
          <asp:TextBox ID="name" runat="server" placeholder="First Name"  ></asp:TextBox><br />


     <asp:RequiredFieldValidator Display="Dynamic"  ID="RequiredFieldValidator6" runat="server"
                ControlToValidate="name" ForeColor="#FF3300" ErrorMessage="It is a mandatory field Can't be left blank"></asp:RequiredFieldValidator>

        <asp:TextBox ID="lname" runat="server"  placeholder="Last Name"></asp:TextBox><br />
         <asp:RequiredFieldValidator Display="Dynamic"  ID="RequiredFieldValidator2" runat="server"
                ControlToValidate="lname" ForeColor="#FF3300" ErrorMessage="It is a mandatory field Can't be left blank"></asp:RequiredFieldValidator>

         <asp:TextBox ID="userid" runat ="server"  placeholder="User Id" ></asp:TextBox><br />
          <asp:RequiredFieldValidator ID="RequiredFieldValidator4"  Display="Dynamic" runat="server"
                ControlToValidate="userid"
                ErrorMessage="It is a mandatory field Can't be left blank" ForeColor="#FF3300"></asp:RequiredFieldValidator>

           <asp:TextBox ID="password" runat="server" EnableViewState="true"  placeholder="Password"
                TextMode="Password" ></asp:TextBox><br />
                 <asp:RequiredFieldValidator ID="RequiredFieldValidator5" Display="Dynamic" runat="server"
                ForeColor="#FF3300" ControlToValidate="password" ErrorMessage="It is a mandatory field Can't be left blank"></asp:RequiredFieldValidator>

                 <asp:TextBox ID="cellno" runat="server"  placeholder="Mobile No"></asp:TextBox><br />
                  <asp:RangeValidator ID="RangeValidator1" runat="server"
                          ForeColor="#FF3300" ControlToValidate="cellno" Display="Dynamic" ErrorMessage="Not in the correct format"
                                 MaximumValue="9999999999" MinimumValue="1000000000" Type="Double"></asp:RangeValidator>

                 <asp:TextBox ID="emailid" runat="server"  placeholder="Email ID"></asp:TextBox> <br />
                   <asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator3" runat="server"
                         ForeColor="#FF3300" ControlToValidate="emailid" ErrorMessage="It is a mandatory field Can't be left blank"></asp:RequiredFieldValidator>

            <asp:RegularExpressionValidator Display="Dynamic" ID="RegularExpressionValidator1" runat="server"
                 ForeColor="#FF3300" ControlToValidate="emailid" ErrorMessage="Not in the correct format i.e abc@domain"
                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator><br>
    <asp:FileUpload ID="FileUpload1" runat="server" />

                    <asp:Button ID="Button1" runat="server" class="btn" Text="Submit" onclick="Button1_Click" />

                       <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
                        </div>
        <script src='http://codepen.io/assets/libs/fullpage/jquery.js'></script>
    </form>
</body>
</html>


my database

SQL
create table User_Register (id numeric identity(1,1),
Fname varchar(100),Lname varchar(100),UserID varchar(100),
Password varchar(50),MobileNo numeric,Email varchar(70),
ProfileImage Image);
select * from User_Register



i m unable to undstnd wht to do now..
Posted
Updated 29-Oct-14 2:16am
v2
Comments
kedar001 29-Oct-14 6:58am
   
try insert command like
cmd.CommandText = "insert into login(UserID,Password) values('" + userid.Text + "','" + password.Text + "')";
Aarti Yadav 29-Oct-14 7:08am
   
i tried but this not working still geting error

Column name or number of supplied values does not match table definition
Aarti Yadav 29-Oct-14 7:02am
   
K let me try..
Aarti Yadav 29-Oct-14 7:07am
   
no this is not working. i tried your sujstn..
Aarti Yadav 29-Oct-14 7:13am
   
any one is der...
kedar001 29-Oct-14 7:19am
   
can you send login table columns
Richard Deeming 29-Oct-14 9:06am
   
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

From your code it looks like you have problem in the following line.

SQL
cmd.CommandText = "insert into login values('" + userid.Text + "','" + password.Text + "')";


Try following as suggested by kedar001 in the comment.

SQL
cmd.CommandText = "insert into login (UserID,Password) values('" + userid.Text + "','" + password.Text + "')";


It is always recomended to use the full signature of INSERT to avoid these and few other kind of errors.
   
Comments
Aarti Yadav 29-Oct-14 7:26am
   
i have try this but still geting error ..

cmd.CommandText = "insert into login (UserID,Password) values('" + userid.Text + "','" + password.Text + "')";
Aarti Yadav 29-Oct-14 7:31am
   
cmd.CommandText = "insert into login values('" + userid.Text + "','" + password.Text + "')";


this is also not working
Suvendu Shekhar Giri 29-Oct-14 7:37am
   
can u please paste the script for table "login". It will help to get the exact solution
Aarti Yadav 29-Oct-14 7:40am
   
this is my table

use torqbolt
create table User_Register (id numeric identity(1,1),
Fname varchar(100),Lname varchar(100),UserID varchar(100),
Password varchar(50),MobileNo numeric,Email varchar(70),
ProfileImage Image);
select * from User_Register
Aarti Yadav 29-Oct-14 7:42am
   
use torqbolt
create table User_Register (id numeric identity(1,1),
Fname varchar(100),Lname varchar(100),UserID varchar(100),
Password varchar(50),MobileNo numeric,Email varchar(70),
ProfileImage Image);
select * from User_Register



this is m table where i cretae my table
Suvendu Shekhar Giri 29-Oct-14 7:46am
   
so, don't you have a "login" table in your database?
Aarti Yadav 29-Oct-14 8:12am
   
hey thnx i got d prblm der is essue in login table and login script ...
Richard Deeming 29-Oct-14 9:07am
   
You've copied the SQL Injection[^] vulnerability from the OP's question.

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Aarti Yadav 30-Oct-14 6:15am
   
okyy thnxxx
there was problem in my login table

cmd.CommandText = "insert into login values('" + userid.Text + "','" + password.Text + "')";

this is my login code

and in m login table der was 3 column and ws passing a data in 2 column.. now its solve

thnx ...

Suvendu Shekhar Giri
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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