Getting following error:
Server Error in '/' Application.
Column name or number of supplied values does not match table definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Column name or number of supplied values does not match table definition.
Source Error:
Line 23: string query = "insert into reg_info values('" + fnametxt.Text + "','" + lnametxt.Text + "','" + dobtxt.Text + "','" + unametxt.Text + "','" + passtxt.Text + "','" + confpasstxt.Text + "','" + ematxt.Text + "','" + confematxt.Text + "','" + statedrop.Text + "','" + citydrop.Text + "')";
Line 24: SqlCommand cmd = new SqlCommand(query, con);
Line 25: cmd.ExecuteNonQuery();
Line 26: con.Close();
Line 27: Console.WriteLine("YOU SUCCESSFULLY REGISTERED");
Source File: c:\Users\Himanshu\Documents\Visual Studio 2012\WebSites\Reg_Page\Default.aspx.cs Line: 25
C# code
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Validate("db");
if (IsValid)
{
string str = "Data Source=(local);Initial Catalog=new1;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
con.Open();
string query = "insert into reg_info values('" + fnametxt.Text + "','" + lnametxt.Text + "','" + unametxt.Text + "','" + passtxt.Text + "','" + confpasstxt.Text + "','" + ematxt.Text + "','" + confematxt.Text + "','" + statedrop.Text + "','" + citydrop.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
Console.WriteLine("YOU SUCCESSFULLY REGISTERED");
}
else { Response.Write("required"); }
}
}
ASPX page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 158px;
margin-left: 40px;
}
.auto-style3 {
width: 262px;
}
.auto-style4 {
width: 158px;
text-align: right;
margin-left: 40px;
}
.auto-style5 {
width: 174px;
}
.auto-style6 {
width: 158px;
height: 23px;
margin-left: 40px;
}
.auto-style7 {
width: 262px;
height: 23px;
}
.auto-style8 {
width: 174px;
height: 23px;
}
.auto-style9 {
height: 23px;
}
.auto-style10 {
width: 158px;
text-align: right;
margin-left: 40px;
height: 26px;
}
.auto-style11 {
width: 262px;
height: 26px;
}
.auto-style12 {
width: 174px;
height: 26px;
}
.auto-style13 {
height: 26px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style4"> </td>
<td class="auto-style3">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
</td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style10">
<asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label>
</td>
<td class="auto-style11">
<asp:TextBox ID="fnametxt" runat="server" Width="180px"></asp:TextBox>
</td>
<td class="auto-style12">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="First name can't be empty" ControlToValidate="fnametxt"></asp:RequiredFieldValidator>
</td>
<td class="auto-style13"></td>
</tr>
<tr>
<td class="auto-style6"></td>
<td class="auto-style7">
</td>
<td class="auto-style8"></td>
<td class="auto-style9"> </td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label9" runat="server" Text="Last Name"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="lnametxt" runat="server" Width="180px"></asp:TextBox>
</td>
<td class="auto-style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Last name can't be empty" ControlToValidate="lnametxt"></asp:RequiredFieldValidator>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label10" runat="server" Text="Date Of Birth"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="dobtxt" runat="server" Width="180px"></asp:TextBox>
<asp:CalendarExtender ID="dobtxt_CalendarExtender" runat="server" Format="dd/MM/yyyy" PopupButtonID="ImageButton1" TargetControlID="dobtxt">
</asp:CalendarExtender>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Images/cal_icon.gif" />
</td>
<td class="auto-style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Date of birth can't be empty" ControlToValidate="dobtxt"></asp:RequiredFieldValidator>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label11" runat="server" Text="User Name"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="unametxt" runat="server" Width="180px"></asp:TextBox>
</td>
<td class="auto-style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="User name can't be empty" ControlToValidate="unametxt"></asp:RequiredFieldValidator>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style6"></td>
<td class="auto-style7"></td>
<td class="auto-style8"></td>
<td class="auto-style9"></td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label5" runat="server" Text="Password"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="passtxt" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
</td>
<td class="auto-style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="Password can't be empty" ControlToValidate="passtxt"></asp:RequiredFieldValidator>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label6" runat="server" Text="Confirm Password"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="confpasstxt" runat="server" TextMode="Password" Width="180px"></asp:TextBox>
</td>
<td class="auto-style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Re-password can't be empty" ControlToValidate="confpasstxt"></asp:RequiredFieldValidator>
</td>
<td>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="passtxt" ControlToValidate="confpasstxt" ErrorMessage="Both Password must be same"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label7" runat="server" Text="Email Address"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="ematxt" runat="server" Width="180px"></asp:TextBox>
</td>
<td class="auto-style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="Email can't be empty" ControlToValidate="ematxt"></asp:RequiredFieldValidator>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label8" runat="server" Text="Confirm Email Address"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="confematxt" runat="server" Width="180px"></asp:TextBox>
</td>
<td class="auto-style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="Re-Email can't be empty" ControlToValidate="confematxt"></asp:RequiredFieldValidator>
</td>
<td>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToCompare="ematxt" ControlToValidate="confematxt" ErrorMessage="Both Email must be same"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label12" runat="server" Text="State"></asp:Label>
</td>
<td class="auto-style3">
<asp:DropDownList ID="statedrop" runat="server" Width="180px">
<asp:ListItem>Please Select</asp:ListItem>
<asp:ListItem>Gujarat</asp:ListItem>
<asp:ListItem>Rajasthan</asp:ListItem>
<asp:ListItem>Karnataka</asp:ListItem>
<asp:ListItem>Maharastra</asp:ListItem>
<asp:ListItem>Delhi</asp:ListItem>
<asp:ListItem>Jammu & Kashmir</asp:ListItem>
<asp:ListItem>Madhya Pradesh</asp:ListItem>
<asp:ListItem>Chhatisgadh</asp:ListItem>
<asp:ListItem>Kerala</asp:ListItem>
<asp:ListItem>Tamil Nadu</asp:ListItem>
<asp:ListItem>Oddisha</asp:ListItem>
<asp:ListItem>Andhra Pradesh</asp:ListItem>
</asp:DropDownList>
</td>
<td class="auto-style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="State can't be empty" ControlToValidate="statedrop"></asp:RequiredFieldValidator>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="Label13" runat="server" Text="City"></asp:Label>
</td>
<td class="auto-style3">
<asp:DropDownList ID="citydrop" runat="server" Width="180px">
<asp:ListItem>Rajkot</asp:ListItem>
<asp:ListItem>Amdavad</asp:ListItem>
<asp:ListItem>Junagadh</asp:ListItem>
<asp:ListItem>Raipur</asp:ListItem>
<asp:ListItem>Bhopal</asp:ListItem>
<asp:ListItem>Delhi</asp:ListItem>
<asp:ListItem>Bangaluru</asp:ListItem>
<asp:ListItem>Chennai</asp:ListItem>
<asp:ListItem>Trivendrum</asp:ListItem>
<asp:ListItem>Madhurai</asp:ListItem>
<asp:ListItem>Mumbai</asp:ListItem>
<asp:ListItem>Pune</asp:ListItem>
<asp:ListItem>Nagpur</asp:ListItem>
<asp:ListItem>Vadodra</asp:ListItem>
<asp:ListItem>Surat</asp:ListItem>
<asp:ListItem>Hindpur</asp:ListItem>
<asp:ListItem>Hubli</asp:ListItem>
<asp:ListItem>Ranchi</asp:ListItem>
<asp:ListItem>Hyderabad</asp:ListItem>
<asp:ListItem>VIZ</asp:ListItem>
</asp:DropDownList>
</td>
<td class="auto-style5">
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ErrorMessage="City can't be empty" ControlToValidate="citydrop"></asp:RequiredFieldValidator>
</td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
<td class="auto-style5"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style6"></td>
<td class="auto-style7">
</td>
<td class="auto-style8"></td>
<td class="auto-style9"></td>
</tr>
</table>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:new1ConnectionString %>" ProviderName="<%$ ConnectionStrings:new1ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [reg_info]"></asp:SqlDataSource>
</form>
</body>
</html>
Sql query
create table reg_info
(first_name varchar(200) not null,last_name varchar(200)not null,dob date not null,user_name varchar(100)not null,password varchar(100)not null,emai_add varchar(100)not null,state varchar(100)not null,city varchar(100)not null
);