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

Krishna GaradRank #191 · 12.8K reputationPosted Jun 14, 2011, 2:20 AM EDT
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");
Dorababu MekaRank #457 · 5.5K reputationPosted Jun 14, 2011, 2:27 AM EDT
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()
Nethra R SRank #6,347 · 269 reputationPosted Jun 14, 2011, 3:29 AM EDT
Nethra R SRank #6,347 · 269 reputationPosted Jun 15, 2011, 1:09 AM EDT
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
Dorababu MekaRank #457 · 5.5K reputationPosted Jun 15, 2011, 1:21 AM EDT
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 SRank #6,347 · 269 reputationPosted Jun 15, 2011, 1:25 AM EDT
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 MekaRank #457 · 5.5K reputationPosted Jun 15, 2011, 1:34 AM EDT
Modify the below code as per your requirement
// Where oMailing is the object name change as per your required
Nethra R SRank #6,347 · 269 reputationPosted Jun 15, 2011, 1:42 AM EDT
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 MekaRank #457 · 5.5K reputationPosted Jun 15, 2011, 2:00 AM EDT
Nethra R SRank #6,347 · 269 reputationPosted Jun 15, 2011, 2:04 AM EDT
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?
Nethra R SRank #6,347 · 269 reputationPosted Jun 15, 2011, 2:53 AM EDT
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
Dorababu MekaRank #457 · 5.5K reputationPosted Jun 15, 2011, 3:08 AM EDT
Krishna GaradRank #191 · 12.8K reputationPosted Jun 15, 2011, 3:10 AM EDT
Nethra R SRank #6,347 · 269 reputationPosted Jun 15, 2011, 3:10 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jun 15, 2011, 3:19 AM EDT
Nethra R SRank #6,347 · 269 reputationPosted Jun 15, 2011, 3:22 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jun 15, 2011, 3:28 AM EDT
Nethra R SRank #6,347 · 269 reputationPosted Jun 15, 2011, 3:33 AM EDT
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 MekaRank #457 · 5.5K reputationPosted Jun 15, 2011, 3:41 AM EDT
Nethra R SRank #6,347 · 269 reputationPosted Jun 15, 2011, 3:53 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jun 15, 2011, 3:58 AM EDT
iInsert in to tbl values (your textbox values or the string values) and let me know
Nethra R SRank #6,347 · 269 reputationPosted Jun 15, 2011, 8:51 PM EDT
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