12. november 2005 - 17:13
#3
hele koden..
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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 giderikkemere
{
/// <summary>
/// Summary description for OpretBruger.
/// </summary>
/// //Data adapter, kombinerer SQL command og Connection string
public class OpretBruger : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox tbNavn;
protected System.Web.UI.WebControls.TextBox tbPass;
protected System.Web.UI.WebControls.TextBox tbRPass;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvNavn;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvPass;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvRPass;
protected System.Web.UI.WebControls.CompareValidator cvRPass;
protected System.Web.UI.WebControls.Button bnOpretBruger;
protected System.Web.UI.WebControls.Button bnRydBrugerFelter;
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 System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.Label Label1;
protected giderikkemere.DataSet1 dataSet11;
protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
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.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.dataSet11 = new giderikkemere.DataSet1();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT ID, Navn, Password FROM Bruger";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "server=MONTYMUZ;uid=sa;pwd=03051996;database=presentation";
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO Bruger(Navn, Password) VALUES (@Navn, @Password); SELECT ID, Navn, Pa" +
"ssword FROM Bruger WHERE (ID = @@IDENTITY)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Navn", System.Data.SqlDbType.VarChar, 75, "Navn"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Password", System.Data.SqlDbType.VarChar, 10, "Password"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE Bruger SET Navn = @Navn, Password = @Password WHERE (ID = @Original_ID) AND (Navn = @Original_Navn OR @Original_Navn IS NULL AND Navn IS NULL) AND (Password = @Original_Password OR @Original_Password IS NULL AND Password IS NULL); SELECT ID, Navn, Password FROM Bruger WHERE (ID = @ID)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Navn", System.Data.SqlDbType.VarChar, 75, "Navn"));
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_ID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ID", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Navn", System.Data.SqlDbType.VarChar, 75, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Navn", 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("@ID", System.Data.SqlDbType.Int, 4, "ID"));
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM Bruger WHERE (ID = @Original_ID) AND (Navn = @Original_Navn OR @Origi" +
"nal_Navn IS NULL AND Navn IS NULL) AND (Password = @Original_Password OR @Origin" +
"al_Password IS NULL AND Password IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ID", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Navn", System.Data.SqlDbType.VarChar, 75, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Navn", 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));
//
// 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", "Bruger", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("Navn", "Navn"),
new System.Data.Common.DataColumnMapping("Password", "Password")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
this.bnOpretBruger.Click += new System.EventHandler(this.bnOpretBruger_Click);
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("da-DK");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
}
#endregion
private void bnOpretBruger_Click(object sender, System.EventArgs e)
{
SqlParameterCollection Params;
if (sqlInsertCommand1 == null)
{
sqlInsertCommand1 = new SqlCommand("Bruger_Insert", sqlConnection1);
sqlInsertCommand1.CommandType = CommandType.StoredProcedure;
Params = sqlInsertCommand1.Parameters;
Params.Add(new SqlParameter("@Navn", SqlDbType.Char, 75));
Params.Add(new SqlParameter("@Password", SqlDbType.Char, 10));
}
Params = sqlInsertCommand1.Parameters;
Params["@Navn"].Value = tbNavn;
Params["@Password"].Value = tbPass;
try
{
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();
}
finally
{
sqlConnection1.Close();
}
}
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
}
/* private void bnRydBrugerFelter_Click(object sender, System.EventArgs e)
{
tbNavn.Text = "";
tbPass.Text = "";
tbRPass.Text = "";
}*/
}