Here is my C# code for a simple web logon form:
After I added the sqlDataAdapter1 , sqlConnection1,
dataSet1 and compile the program, there was build error
which said it did not find dataSet1. When I saw the aspx
design page, the DataSet11 was disappeared.
Were my steps wrong? And, where can I find a simple
example to test the connection with ms sql server with
asp.net?
Thanks
Expand|Select|Wrap|Line Numbers
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Web;
- using System.Web.SessionState;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.HtmlControls;
- namespace sampleapplication
- {
- /// <summary>
- /// Summary description for WebForm1.
- /// </summary>
- public class sampleapplication : System.Web.UI.Page
- {
- protected System.Web.UI.WebControls.Label
- Label2;
- protected System.Web.UI.WebControls.Label
- Label3;
- protected
- System.Web.UI.WebControls.TextBox txtUserName;
- protected
- System.Web.UI.WebControls.TextBox txtPassword;
- protected System.Web.UI.WebControls.Button
- btnSubmit;
- protected
- System.Web.UI.WebControls.RequiredFieldValidator
- RequiredFieldValidator1;
- protected
- System.Web.UI.WebControls.RequiredFieldValidator
- RequiredFieldValidator2;
- protected System.Web.UI.WebControls.Label
- lblMessage;
- protected
- System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
- protected System.Data.SqlClient.SqlCommand
- sqlSelectCommand1;
- protected System.Data.SqlClient.SqlCommand
- sqlInsertCommand1;
- protected System.Data.SqlClient.SqlCommand
- sqlUpdateCommand1;
- protected System.Data.SqlClient.SqlCommand
- sqlDeleteCommand1;
- protected
- System.Data.SqlClient.SqlConnection sqlConnection1;
- protected sampleapplication.DataSet1
- dataSet11;
- protected System.Web.UI.WebControls.Label
- Label1;
- private void Page_Load(object sender,
- System.EventArgs e)
- {
- // Put user code to initialize the
- page here
- }
- #region Web Form Designer generated code
- override protected void OnInit(EventArgs e)
- {
- //
- // CODEGEN: This call is required
- by the ASP.NET Web Form Designer.
- //
- InitializeComponent();
- base.OnInit(e);
- }
- /// <summary>
- /// Required method for Designer support -
- do not modify
- /// the contents of this method with the
- code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.sqlDataAdapter1 = new
- System.Data.SqlClient.SqlDataAdapter();
- this.sqlDeleteCommand1 = new
- System.Data.SqlClient.SqlCommand();
- this.sqlConnection1 = new
- System.Data.SqlClient.SqlConnection();
- this.sqlInsertCommand1 = new
- System.Data.SqlClient.SqlCommand();
- this.sqlSelectCommand1 = new
- System.Data.SqlClient.SqlCommand();
- this.sqlUpdateCommand1 = new
- System.Data.SqlClient.SqlCommand();
- this.dataSet11 = new
- sampleapplication.DataSet1();
- ((System.ComponentModel.ISupportInitialize)
- (this.dataSet11)).BeginInit();
- this.btnSubmit.Click += new
- System.EventHandler(this.btnSubmit_Click);
- //
- // sqlDataAdapter1
- //
- this.sqlDataAdapter1.DeleteCommand
- = this.sqlDeleteCommand1;
- this.sqlDataAdapter1.InsertCommand
- = this.sqlInsertCommand1;
- this.sqlDataAdapter1.SelectCommand
- = this.sqlSelectCommand1;
- this.sqlDataAdapter1.TableMappings.AddRange(new
- System.Data.Common.DataTableMapping[] {
- new
- System.Data.Common.DataTableMapping("Table", "logon", new
- System.Data.Common.DataColumnMapping[] {
- new System.Data.Common.DataColumnMapping
- ("username", "username"),
- new System.Data.Common.DataColumnMapping
- ("password", "password")})});
- this.sqlDataAdapter1.UpdateCommand
- = this.sqlUpdateCommand1;
- //
- // sqlDeleteCommand1
- //
- this.sqlDeleteCommand1.CommandText
- = "DELETE FROM logon WHERE (username = @Original_username)
- AND (password = @Original" +
- "_password)";
- this.sqlDeleteCommand1.Connection
- = this.sqlConnection1;
- this.sqlDeleteCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Original_username",
- System.Data.SqlDbType.VarChar, 10,
- System.Data.ParameterDirection.Input, false, ((System.Byte)
- (0)), ((System.Byte)(0)), "username",
- System.Data.DataRowVersion.Original, null));
- this.sqlDeleteCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Original_password",
- System.Data.SqlDbType.VarChar, 10,
- System.Data.ParameterDirection.Input, false, ((System.Byte)
- (0)), ((System.Byte)(0)), "password",
- System.Data.DataRowVersion.Original, null));
- //
- // sqlConnection1
- //
- this.sqlConnection1.ConnectionString = "data
- source=SERVER;initial catalog=sampledatabase;persist
- security info=Fal" +
- "se;user id=sa;workstation
- id=SERVER;packet size=4096";
- //
- // sqlInsertCommand1
- //
- this.sqlInsertCommand1.CommandText
- = "INSERT INTO logon(username, password) VALUES
- (@username, @password); SELECT usern" +
- "ame, password FROM logon
- WHERE (username = @username)";
- this.sqlInsertCommand1.Connection
- = this.sqlConnection1;
- this.sqlInsertCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@username",
- System.Data.SqlDbType.VarChar, 10, "username"));
- this.sqlInsertCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@password",
- System.Data.SqlDbType.VarChar, 10, "password"));
- //
- // sqlSelectCommand1
- //
- this.sqlSelectCommand1.CommandText
- = "SELECT username, password FROM logon WHERE (username =
- @username)";
- this.sqlSelectCommand1.Connection
- = this.sqlConnection1;
- this.sqlSelectCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@username",
- System.Data.SqlDbType.VarChar, 10, "username"));
- //
- // sqlUpdateCommand1
- //
- this.sqlUpdateCommand1.CommandText
- = "UPDATE logon SET username = @Param1, password =
- @password WHERE (username = @Orig" +
- "inal_username) AND
- (password = @Original_password); SELECT username, password
- FR" +
- "OM logon WHERE (username
- = @username)";
- this.sqlUpdateCommand1.Connection
- = this.sqlConnection1;
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Param1",
- System.Data.SqlDbType.VarChar, 10, "username"));
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@password",
- System.Data.SqlDbType.VarChar, 10, "password"));
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Original_username",
- System.Data.SqlDbType.VarChar, 10,
- System.Data.ParameterDirection.Input, false, ((System.Byte)
- (0)), ((System.Byte)(0)), "username",
- System.Data.DataRowVersion.Original, null));
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Original_password",
- System.Data.SqlDbType.VarChar, 10,
- System.Data.ParameterDirection.Input, false, ((System.Byte)
- (0)), ((System.Byte)(0)), "password",
- System.Data.DataRowVersion.Original, null));
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@username",
- System.Data.SqlDbType.VarChar, 10, "username"));
- //
- // dataSet11
- //
- this.dataSet11.DataSetName
- = "DataSet1";
- this.dataSet11.Locale = new
- System.Globalization.CultureInfo("zh-CN");
- this.dataSet11.Namespace
- = "http://www.tempuri.org/DataSet1.xsd";
- this.Load += new
- System.EventHandler(this.Page_Load);
- ((System.ComponentModel.ISupportInitialize)
- (this.dataSet11)).EndInit();
- }
- #endregion
- private void btnSubmit_Click(object
- sender, System.EventArgs e)
- {
- sqlConnection1.Open();
- sqlDataAdapter1.SelectCommand.Parameters
- [0].Value=txtUserName.Text.Trim();
- sqlDataAdapter1.Fill
- (dataSet11, "UserDetails");
- if (dataSet11.Tables
- ["UserDetails"].Rows.Count==0)
- {
- lblMessage.Text="Invalid
- user name";
- }
- else
- {
- if (dataSet11.Tables
- ["UserDetails"].Rows[0][1].ToString().Trim()
- ==txtPassword.Text.Trim())
- lblMessage.Text="Welcome + " + txtUserName.Text;
- else
- lblMessage.Text="Invalid PASSWORD";
- }
- sqlConnection1.Close();
- }
- }
- }