Hi,
I am new to OOPS concept. I have created a asp.net page which is a new user registration page. At the button click i want to call a method to handle this.. I have created a class registercls in app_code folder and i want to add some methods. I have these fields like firstname,lastname,city,phone etc.. How to write a method to handle this.. At button click i want to call the method.. plzz help
Loading
Nethra R SPosted Jun 16, 2011, 12:51 AM
Plzz check this method i have written and let me know if it is correct plzzz:
public bool validateuser(string username,string password)
{
bool returnres = false;
try
{
if (CON.State != ConnectionState.Open)
{
CON.Open();
CMD = new SqlCommand("select UserName,Password from login_master", CON);
DTR = CMD.ExecuteReader(CommandBehavior.CloseConnection);
if (DTR.HasRows)
{
while (DTR.Read())
{
if (DTR["UserName"].ToString() == username && DTR["Password"].ToString() == obj.base64Encode(password))
{
returnres = true;
break;
}
else
{
returnres = false;
}
}
}
}
DTR.Close();
// if (Session["user"] != null) Response.Redirect("~/home.aspx");
CON.Close();
}
catch
{ }
finally
{
CON.Close();
}
return returnres;
}
==================Here selectrecord is a stored procedure
create procedure [dbo].[selectrecord]
@UserName varchar(50),
@Password varchar(50),
@FName varchar(50),
@LName varchar(50),
@Address varchar(200),
@Phone varchar(50),
@EmailID varchar(50),
@City varchar(50),
@Pincode varchar(50)
AS
SELECT * FROM login_master
Dorababu MekaPosted Jun 15, 2011, 7:58 AM
iInsert in to tbl values (your textbox values or the string values) and let me know
Nethra R SPosted Jun 15, 2011, 7:53 AM
Dorababu MekaPosted Jun 15, 2011, 7:41 AM
Nethra R SPosted Jun 15, 2011, 7:33 AM
It is not passing the values.. I used the try catch block.. It goes to the catch block immediately..
The stored procedure is:
Create procedure [dbo].[insertrecord]
@UserName varchar(50),
@Password varchar(50),
@FName varchar(50),
@LName varchar(50),
@Address varchar(200),
@Phone varchar(50),
@EmailID varchar(50),
@City varchar(50),
@Pincode varchar(50)
AS
INSERT INTO login_master(UserName,Password,FName,LName,Address,Phone,EmailID,City,Pincode)VALUES(@UserName,@Password,@FName,@LName,@Address,@Phone,@EmailID,@City,@Pincode)
Dorababu MekaPosted Jun 15, 2011, 7:28 AM
Nethra R SPosted Jun 15, 2011, 7:22 AM
Dorababu MekaPosted Jun 15, 2011, 7:19 AM
Nethra R SPosted Jun 15, 2011, 7:10 AM
Krishna GaradPosted Jun 15, 2011, 7:10 AM
Dorababu MekaPosted Jun 15, 2011, 7:08 AM
Nethra R SPosted Jun 15, 2011, 6:53 AM
public partial class Default2 : System.Web.UI.Page
{
registercls obj = new registercls();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnregisteruser_Click(object sender, EventArgs e)
{
if (obj.registeruser(txtfname.Text, txtlname.Text, txtaddress.Text, txtphone.Text, txtemail.Text, txtcity.Text, txtpincode.Text, txtuser.Text, txtpassword.Text))
{
lblmsg.Text = "User registered successfully!";
}
else
{
lblmsg.Text = "Registration failed!";
}
}
}
======================================================
Is this correct? Anyways i am not able to insert the values in the table... Plzz correct me
Nethra R SPosted Jun 15, 2011, 6:04 AM
How to call this registeruser method and how to pass values? PLzz tell me.. At button click how to pass values..
Is it like this?
object.registeruser(txtfname.text,txtlname.text................................etc); and method is bool so how to?
Dorababu MekaPosted Jun 15, 2011, 6:00 AM
Nethra R SPosted Jun 15, 2011, 5:42 AM
public bool registeruser(string fname, string lname, string address, string phone, string email, string city, string pincode, string username, string password)
{
bool returnres=false;
try
{
if (CON.State != ConnectionState.Open)
{
CON.Open();
CMD = new SqlCommand("insertrecord", CON);
CMD.CommandType = CommandType.StoredProcedure;
CMD.Parameters.AddWithValue("UserName", fname);
CMD.Parameters.AddWithValue("@Password", password);
CMD.Parameters.AddWithValue("@FName", fname);
CMD.Parameters.AddWithValue("@LName", lname);
CMD.Parameters.AddWithValue("@Address", address);
CMD.Parameters.AddWithValue("@Phone", phone);
CMD.Parameters.AddWithValue("@EmailID", email);
CMD.Parameters.AddWithValue("@City", city);
CMD.Parameters.AddWithValue("@Pincode", pincode);
CMD.ExecuteNonQuery();
returnres = true;
}
}
catch
{ }
finally
{
CON.Close();
}
return returnres;
}
Dorababu MekaPosted Jun 15, 2011, 5:34 AM
Modify the below code as per your requirement
// Where oMailing is the object name change as per your required
Nethra R SPosted Jun 15, 2011, 5:25 AM
U mean i must write that code after
public bool registeruser(string fname, string lname, string address, string phone, string email, string city, string pincode, string username, string password)
{
bool returnres=false;
//HERE SHOULD I WRITE THE CODE WHICH U R TELLING?
OK then at the resisteration button click Should i create an object of that class and call the method and just pass the values?
Dorababu MekaPosted Jun 15, 2011, 5:21 AM
Example:
try
{
if (m_oConn.State != ConnectionState.Open)
{
m_oConn.Open();
}
m_oCmd = new SqlCommand(StoredProcNames.Mailing_uspAddMailingList, m_oConn);
m_oCmd.CommandType = CommandType.StoredProcedure;
m_oCmd.Parameters.AddWithValue("@CONTACTFirstName", FirstName);
m_oCmd.Parameters.AddWithValue("@CONTACTLASTNAME", LastName);
m_oCmd.Parameters.AddWithValue("@CONTACTEMAIL", Email);
m_oCmd.Parameters.AddWithValue("@CONTACTPHONE", Phone);
m_oCmd.Parameters.AddWithValue("@CONTACTROLE", Role);
if (m_oConn.State == ConnectionState.Closed)
{
m_oConn.Open();
}
if ((m_oCmd.ExecuteNonQuery()) > 0)
{
this.m_bFlag = true;
}
}
catch (SqlException oSqlEx)
{
}
catch (Exception oEx)
{
}
finally
{
m_oConn.Close(); //always close the connection
}
Nethra R SPosted Jun 15, 2011, 5:09 AM
I have started like this.. I want an adduser method and i want to call this method at button click and just pass the values...
public bool registeruser(string fname, string lname, string address, string phone, string email, string city, string pincode, string username, string password)
{
bool returnres=false;
return returnres;
}
In this method i am not understanding what to write. Plzz help
Also if someone can tell me the advantage of using methods and properties i will be grateful to u`ll..Thanks
Nethra R SPosted Jun 14, 2011, 7:29 AM
Dorababu MekaPosted Jun 14, 2011, 6:27 AM
Private string strFirstName;
Private string strLastName;
Private string strCity; and so on as per your requirement
Initializing
public string FirstName
{
get { return strFirstName; }
set { strFirstName= value; }
}
Like this you have to declare your public methods to use in the code
Methods
public bool Add()
{
// your code here
}
Call this method using name of your code file by creating an object
Sample sam=new Sample();
and call your function as sam.Add()
Krishna GaradPosted Jun 14, 2011, 6:20 AM
string _fname,_lname;
puplic string Fname{get;set;}
public string Lname{get;set;}
void Method()
{
//code
}
Using method parameter
void Method(string _fname,string _lname)
{
//code
}
in your button click event
registercls obj=new registercls();
// if using properties way
obj.Fname="Value";
obj.Lname="Value";
obj.Method();
//if using Method Parameter Way
obj.Method("Value","Value");