在执行当前Web请求期间发生了未处理的异常 [英] An unhandled exception occurred during the execution of the current web request

查看:95
本文介绍了在执行当前Web请求期间发生了未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取以下错误:



'/'应用程序中的服务器错误。

列名或提供的值与表定义不匹配。

描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.Data.SqlClient.SqlException:列名或编号提供的值与表定义不匹配。



来源错误:





第23行:字符串查询=插入reg_info值('+ fnametxt.Text +','+ lnametxt.Text +','+ dobtxt.Text +','+ unametxt.Text +','+ passtxt.Text +','+ confpasstxt.Text +','+ ematxt.Text +','+ confematxt.Text +','+ statedrop.Text +','+ citydrop.Text +');

第24行:SqlCommand cmd = new SqlCommand(query,con);

第25行:cmd .ExecuteNonQuery();

第26行:con.Close();

第27行:Console.WriteLine(你已经成功注册);





源文件:c:\ Users \\ \\ Himanshu \Documents\Visual Studio 2012 \WebSites\Reg_Page\Default.aspx.cs行:25



C#代码



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页面:





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">&nbsp;</td>
                <td class="auto-style3">
                    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
                    </asp:ToolkitScriptManager>
                </td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</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">
                    &nbsp;</td>
                <td class="auto-style8"></td>
                <td class="auto-style9">&nbsp;</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>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</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>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</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>&nbsp;</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>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</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">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</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>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</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">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</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 &amp; 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>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</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>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">
                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
                </td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style5">&nbsp;</td>
                <td>&nbsp;</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









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
);

推荐答案

ConnectionStrings:new1ConnectionString %>\" ProviderName=\"<%
ConnectionStrings:new1ConnectionString %>" ProviderName="<%


ConnectionStrings:new1ConnectionString.ProviderName %>\" SelectCommand=\"SELECT * FROM [reg_info]\"></asp:SqlDataSource>
</form>
</body>
</html>
ConnectionStrings:new1ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [reg_info]"></asp:SqlDataSource> </form> </body> </html>













Sql query









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
);


Put a debugger before cmd.ExecuteNonQuery();.

Add a watch to get the value of query. Now run this value in your SQL query analyzer. You will find the issue.



Basically there is something wrong with your insert query. Either the number of parameters, or sequence or datatype isn’t correct. Correct your query, change the code accordingly and it’s done!
Put a debugger before cmd.ExecuteNonQuery();.
Add a watch to get the value of query. Now run this value in your SQL query analyzer. You will find the issue.

Basically there is something wrong with your insert query. Either the number of parameters, or sequence or datatype isn't correct. Correct your query, change the code accordingly and it's done!


这篇关于在执行当前Web请求期间发生了未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆