using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;
public partial class createUser : System.Web.UI.Page{ public string GetConnectionString() { return System.Configuration.ConfigurationManager.ConnectionStrings["mfwamba"].ConnectionString; }
private void execution(string name, string username, string password, string emailid) { SqlConnection conn = new SqlConnection(GetConnectionString); string sql = "INSERT INTO myTb(name, username, password, emailid) VALUES " + " (@name, @username, @password, @emailid)"; try { conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter[] pram = new SqlParameter[4];
pram[0] = new SqlParameter("@name", SqlDbType.VarChar, 50); pram[1] = new SqlParameter("@username", SqlDbType.VarChar, 50); pram[2] = new SqlParameter("@password", SqlDbType.VarChar, 50); pram[3] = new SqlParameter("@email", SqlDbType.VarChar, 10);
pram[0].Value = name; pram[1].Value = username; pram[2].Value = password; pram[3].Value = emailid;
for (int i = 0; i < pram.Length; i++) { cmd.Parameters.Add(pram[i]); } cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex_msg) { string msg ="Error occured while inserting"; msg+= ex_msg.Message; throw new Exception(msg); } finally { conn.Close(); } } protected void create_Click(object sender, EventArgs e) { SqlDataSource sds = new SqlDataSource(); sds.ConnectionString = Configuration.ConnectionStrings["mfwamba"].ToString(); sds.SelectParameters.Add("name",TypeCode.String, this.name.Text); sds.SelectParameters.Add("username",TypeCode.String, this.username.Text); sds.SelectParameters.Add("password",TypeCode.String, this.password.Text); sds.SelectParameters.Add("emailid",TypeCode.String, this.emailid.Text);
sds.SelectCommand = "SELECT * FROM [myTab] WHERE [username] = @username";
DataView dv = (DataView)sds.Select(DataSourceSelectArguments.Empty);
if(dv.Count != 0) { this.lblinfo.ForeColor = System.Drawing.Color.Red; this.lblinfo.Text = "The user already Exist"; return; } else { execution(name.Text,username.Text,password.Text,emailid.Text); this.lblinfo.Text="New User Profile has been created you can login now"; this.name.Text = ""; this.username.Text = ""; this.password.Text = ""; this.emailid.Text = ""; } } }--------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="createUser.aspx.cs" Inherits="createUser" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <br><asp:Label ID="Label1" runat="server" Text="Name"></asp:Label></br> <asp:TextBox ID="TextBox1" runat="server" style="margin-left: 50px"></asp:TextBox> <br><asp:Label ID="Label2" runat="server" Text="Username"></asp:Label></br> <asp:TextBox ID="TextBox2" runat="server" style="margin-left: 26px"></asp:TextBox> <br><asp:Label ID="Label3" runat="server" Text="Password"></asp:Label></br> <asp:TextBox ID="TextBox3" textmode="Password" runat="server" style="margin-left: 27px">Password</asp:TextBox> <br><asp:Label ID="Label4" runat="server" Text="Email"></asp:Label></br> <asp:TextBox ID="TextBox4" runat="server" style="margin-left: 53px"></asp:TextBox> <!--<asp:Button ID="create_Click" runat="server" Text="Create User" />--> <asp:Label ID="lblinfo" runat="server" Text="Label"></asp:Label>
<br> </br>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display."> <Columns> <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" /> <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" /> <asp:BoundField DataField="username" HeaderText="username" SortExpression="username" /> <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" /> <asp:BoundField DataField="emailed" HeaderText="emailed" SortExpression="emailed" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MfwambaConnectionString1 %>" DeleteCommand="DELETE FROM [myTb] WHERE [id] = @id" InsertCommand="INSERT INTO [myTb] ([name], [username], [password], [emailed]) VALUES (@name, @username, @password, @emailed)" ProviderName="<%$ ConnectionStrings:MfwambaConnectionString1.ProviderName %>" SelectCommand="SELECT [id], [name], [username], [password], [emailed] FROM [myTb]" UpdateCommand="UPDATE [myTb] SET [name] = @name, [username] = @username, [password] = @password, [emailed] = @emailed WHERE [id] = @id"> <DeleteParameters> <asp:Parameter Name="id" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="username" Type="String" /> <asp:Parameter Name="password" Type="String" /> <asp:Parameter Name="emailed" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="username" Type="String" /> <asp:Parameter Name="password" Type="String" /> <asp:Parameter Name="emailed" Type="String" /> <asp:Parameter Name="id" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> </div> </form></body></html>