在注册中获取错误通过webform在SQL服务器数据库表中插入值TEXTBOX

问题描述:

您好我正在尝试使用sql server架构在页面中进行日志记录以及使用3层体系结构在asp.net中注册页面。我能够从sql server数据库中获取数据,这是我在数据库中创建表的时候手动插入的,我可以在我的登录页面中使用它。在注册中获取错误通过webform在SQL服务器数据库表中插入值TEXTBOX

我也创建了一个注册页面,但我无法从注册webform文本框到sqlserver数据库中获取值我收到一些错误,请亲切地帮助我解决这个问题。

我给的SQL Server连接字符串在web.config中

我的SQL Server表创建代码

CREATE TABLE LOGINDETAILS 
(USERID VARCHAR(50), 
PASSWORD VARCHAR (50) 
); 
INSERT INTO LOGINDETAILS (USERID,PASSWORD) values('sam', 'pass'); 

web.config中的连接字符串代码

<?xml version="1.0"?> 

<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=169433 
    --> 

<configuration> 
    <connectionStrings> 
    <add name="DBcon" connectionString="Data Source=P3A-B1YH882\SQLSERVER;Initial Catalog=master;Integrated Security=True" providerName="System.Data.SqlClient"/> 
    </connectionStrings> 

    <system.web> 
     <compilation debug="true" targetFramework="4.5" /> 
     <httpRuntime targetFramework="4.5" /> 
    </system.web> 

</configuration> 

我的业务层/中间层鳕鱼Ë

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Configuration; 
using System.Data; 
using DataAcess; 
using System.Data.SqlClient; 
using System.Data.Sql; 

namespace middlelayer 
{ 
    public class UserBO 
    { 
     private string _UserName = " "; 

     public string UserName 
     { 
      get { return _UserName; } 
      set { _UserName = value; } 
     } 
     private string _Password = " "; 

     public string Password 
     { 
      get { return _Password; } 
      set { _Password = value; } 
     } 

     DataA da = new DataA(); 

     public bool getUser() 
     { 
      if (da.IsValid(UserName, Password).Tables[0].Rows.Count == 0) 
      { 
       return false; 


      } 

      else 
      { 
       return true; 


      } 
     } 
    } 
} 

我datAccess层代码

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Data.Sql; 

namespace DataAcess 
{ 
    public class DataA 
    { 

     string conString = ConfigurationManager.ConnectionStrings["DBcon"].ToString(); 
     public DataSet IsValid(string UserName, string Password) 
     { 
      SqlConnection con = new SqlConnection(conString); 
      con.Open(); 
      SqlCommand cmd = new SqlCommand("SELECT * FROM LOGINDETAILS WHERE USERID ='" + UserName + "' and PASSWORD= '" + Password + "'", con); 
      DataSet ds = new DataSet(); 
      SqlDataAdapter sda = new SqlDataAdapter(cmd); 
      sda.Fill(ds); 
      return ds; 

     } 
    } 
} 

我的登录页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="WebApplication4.login" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body style="height: 277px"> 
    <form id="form1" runat="server"> 
    <div> 

     <asp:Label ID="lbluserid" runat="server" BackColor="#FFFF99" BorderStyle="Ridge" Height="17px" Text="User ID" Width="52px"></asp:Label> 
     <asp:TextBox ID="txtuserid" runat="server" BackColor="#99FFCC" style="margin-left: 122px"></asp:TextBox> 
&nbsp;&nbsp; 
     <br /> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

    </div> 
     <p> 
      <asp:Label ID="lblpassword" runat="server" BackColor="#FFFF99" BorderStyle="Ridge" Text="Password"></asp:Label> 
&nbsp;<asp:TextBox ID="txtpassword" TextMode="Password" runat="server" BackColor="#99FFCC" style="margin-left: 110px" ></asp:TextBox> 
     </p> 
     <p> 
      &nbsp;</p> 
     <asp:Button ID="btnlogin" runat="server" BackColor="#33CCFF" BorderStyle="Ridge" OnClick="btnlogin_Click" style="margin-left: 78px" Text="Login" Width="107px" /> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <p> 
      &nbsp;</p> 
     <asp:Label ID="Label1" runat="server" Text="NOT REGISTERED ??"></asp:Label> 
     &nbsp;<asp:HyperLink ID="HyperLink1" runat="server" BorderStyle="Outset" NavigateUrl="~/sign_up.aspx">SIGN UP</asp:HyperLink> 
    </form> 
</body> 
</html> 

*我的注册页面代码*

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="sign_up.aspx.cs" Inherits="WebApplication4.sign_up" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 

    </div> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:Label ID="lblssignup" runat="server" BackColor="#FF99CC" Text="SIGN UP"></asp:Label> 
     <br /> 
     <br /> 
     <p> 
      <asp:Label ID="lblsuserid" runat="server" Text="ENTER USER ID"></asp:Label> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 

      <asp:TextBox ID="TextBox1" runat="server" style="margin-bottom: 0px"></asp:TextBox> 

     </p> 
     <asp:Label ID="lblspassword" runat="server" Text="ENTER PASSWORD"></asp:Label> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
     <p> 
      <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" Width="66px" /> 
     </p> 
    </form> 
</body> 
</html> 

注册页面按钮的代码将数据输入到SQL Server数据库上按一下按钮

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.Data; 
using middlelayer; 
namespace WebApplication4 
{ 
    public partial class sign_up : System.Web.UI.Page 
    { 
     string conString = ConfigurationManager.ConnectionStrings["DBcon"].ToString(); 

     protected void Page_Load(object sender, EventArgs e) 
     { 
     } 
     protected void Button1_Click(object sender, EventArgs e) 
     { 
      SqlConnection con = new SqlConnection(conString); 
      con.Open(); 
      string ins= "Insert into [LOGINDETAILS](USERID, PASSWORD) VALUES ('" +TextBox1.Text+ "' , '" +TextBox2.Text+ "')"; 
       SqlCommand com = new SqlCommand(ins,con); 
        DataSet du = new DataSet(); 
      SqlDataAdapter sdi = new SqlDataAdapter(com); 
      sdi.Fill(du); 
      con.Close(); 

      } 


     } 
    } 

我得到的错误在这最后的代码只是注册按钮,它是不能够插入值的注册网络表单文本框到SQL Server数据库表,也没有反映出我想在sql server TABLE中添加的真实值,使用注册webform并且还保存它。它发送一些错误值。请好心帮助我。

下面是日志的图像,以及注册页面仅供参考

LOGIN PAGE WEB FORM

SIGN UP PAGE WEBFORM

请帮助解决这个问题,

+0

而不是说插入logindetails,textbox1。在循环前获取变量中的textbox1数据,然后使用变量将其添加到logindetails中 – Tomm

+0

您已经采取了有用的格式和很好的问题技巧,例如添加代码,大小写,粗体等,并且已经过时了。 –

+0

请你详细说明一下我没有清楚地理解你? Tomm – saurabh255

试试这个:

SqlConnection con = new SqlConnection(conString); 
    con.Open(); 

    string ins= "Insert into [LOGINDETAILS](USERID, PASSWORD) VALUES (@param1 , @param2)"; 
    SqlCommand cmd = new SqlCommand(ins,con); 
    cmd.Parameters.Add("@param1", SqlDbType.Varchar, 50).value = TextBox1.Text; 
    cmd.Parameters.Add("@param2", SqlDbType.Varchar, 50).value = TextBox2.Text; 
    cmd.CommandType = CommandType.Text; 
    cmd.ExecuteNonQuery(); 
    con.Close() 
+0

正常工作感谢您的帮助 – saurabh255