检查aspx.vb Web表单中是否存在sql记录

时间:2013-04-05 02:43:53

标签: asp.net sql vb.net

我在visual studio 2010上做了大约一个星期的项目。我正在使用vb和一个简单的SQL数据库。我正在尝试创建一个登录页面,检查客户表中是否有指定电子邮件和密码的记录。我已经达到了调试深度的程度,有人能看到导致错误的原因吗?

Login Web表单的Login.aspx.vb文件如下所示:

Imports LoginTableAdapters

Partial Class Login
    Inherits System.Web.UI.Page

Dim LoginAdapter As LoginTableAdapter

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Dim email As String
    Dim pass As String
    LoginAdapter = New LoginTableAdapter

    email = TextBox1.Text
    pass = TextBox2.Text

    If Me.LoginAdapter.QueryLogin(email, pass) Then
        Label1.Visible = True
    End If

    End Sub
End Class

QueryLogin()的SQL如下所示:

SELECT        custemail, custpassword
FROM            customer
WHERE        (custemail = @Param1) AND (custpassword = @Param2)

这是仅在输入正确的用户/通行证组合时发生的错误:

Server Error in '/DinnerNow' Application.
Input string was not in a correct format.
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.FormatException: Input string was not in a correct format.

Source Error:

The source code that generated this unhandled exception can only be shown when compiled in debug mode. To enable this, please follow one of the below steps, then request the URL:

1. Add a "Debug=true" directive at the top of the file that generated the error. Example:

  <%@ Page Language="C#" Debug="true" %>

or:

2) Add the following section to the configuration file of your application:

<configuration>
   <system.web>
       <compilation debug="true"/>
   </system.web>
</configuration>

Note that this second technique will cause all files within a given application to be compiled in debug mode. The first technique will cause only that particular file to be compiled in debug mode.

Important: Running applications in debug mode does incur a memory/performance overhead. You should make sure that an application has debugging disabled before deploying into production scenario.

Stack Trace:


[FormatException: Input string was not in a correct format.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +181
   Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value) +147

[InvalidCastException: Conversion from string "test@test.test" to type 'Boolean' is not valid.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(String Value) +337
   Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(Object Value) +1304756
   Login.Button1_Click(Object sender, EventArgs e) +104
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9553178
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

1 个答案:

答案 0 :(得分:1)

你在做什么

If Me.LoginAdapter.QueryLogin(email, pass)

这意味着返回给它的值应该是布尔类型。但是你的sql查询正在返回custemail,custpassword。哪种是字符串类型。因此,boolean不能与字符串类型进行比较。

将您的sql更改为

if((SELECT   Count(*) FROM customer WHERE (custemail = @Param1) AND (custpassword = @Param2))>0)
   Select Cast(1 as bit) IsCustomerExist
Else
    Select Cast(0 as bit) IsCustomerExist