2017-10-06 9 views
0

こんにちは私は、SQL Serverアーキテクチャを使用して3層アーキテクチャを使用してasp.netのページにサインインしてサインインしようとしています。データベースのテーブル作成中に手動で挿入したSQL Serverデータベースからデータを取得でき、ログインページで使用できるようになりました。SQLサーバーに値を挿入するためのサインアップページを取得するTEXTBOX

サインアップページも作成しましたが、webform textboxからsqlserverデータベースに値を渡すことができません。

私はweb.configファイルでSQL Serverの接続文字列を与えている

私の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; 

     } 
    } 
} 

MYログインページCODE

<%@ 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> 

* MY SIGN UPページコード*

<%@ 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> 

SIGN UPボタンクリック

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データベースにデータを入力するためのページボタンのコードを、私は唯一の値を挿入することはできないボタンをサインアップし、この最後のコード内のエラーを取得していますwebform TextboxをSQL Serverのデータベーステーブルに登録して、SQLフォームのwebformを使用してSQL Server TABLEに追加したい実際の値を反映しないで保存してください。いくつかのエラー値を送信しています。親切に私にこれを手伝ってください。 BELOW

REFERENCE

LOGIN PAGE WEB FORM

SIGN UP PAGE WEBFORM

のログOF IMAGESならびにSIGNのUPページが親切、この問題を解決するための

+0

logindetailsに挿入するというより、textbox1。ループの前の変数にtextbox1データを取得し、変数を使用してlogindetailsに追加してください。 – Tomm

+0

コードを追加したり、大文字と小文字を区別したり、外に出てしまったりといった便利な形式をとっています。 –

+0

ちょっと丁寧に教えてください。私はあなたをはっきりと知りませんか? Tomm – saurabh255

答えて

0

はこれを試してみてください役立ちます:

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

関連する問題