Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to declare all parameters in class file that is contained in app code folder. Also i want connection to be open and close in a single class file only. only on button click i want to execute all methods and events. my primary aim is to insert records in database and get them back on gridview. but i also want to keep code behind and class file different . my code in clas file is as follows:
C#
using System.Data.SqlClient;

public class Class1
{
    private int _Event;
    private int _EmpId;
    private string _EmpName;
    private int _Salary;
    private string _City;

    public int Event
    {
        get { return _Event; }
        set { Event = value; }
    }

    public int EmpId
    {
        get { return _EmpId; }
        set { EmpId = value; }
    }

    public string EmpName
    {
        get { return _EmpName; }
        set { EmpName = value; }
    }

    public int Salary
    {
        get { return _Salary; }
        set { Salary = value; }
    }

    public string City
    {
        get { return _City; }
        set { City = value; }
    }

    public Class1()
    {
        SqlConnection connect = new SqlConnection();
        SqlCommand command = new SqlCommand();
        command.Parameters.Add("@EmpId", SqlDbType.Int).Value = a;
        command.Parameters.Add("@Emp_name", SqlDbType.NVarChar).Value = b;
        command.Parameters.Add("@Salary", SqlDbType.Int).Value = c;
        command.Parameters.Add("@City", SqlDbType.NVarChar).Value = d;
        connect.Open();
        command.ExecuteNonQuery();
        command.Connection.Close();
    }

code in aspx.cs is as:
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       Class1 obj = new Class1();

       int a = Convert.ToInt32(txtEmpId.Text);
       string b = txtEmpName.Text;
       int c = Convert.ToInt32(txtSalary.Text);
       string d = txtCity.Text;


but in class file a,b,c,d are showing error that a etc does not exist in current context.Why? what is problem ?
Posted
Updated 7-Jun-13 1:37am
v3

1 solution

Hello R,

How do you expect Class1 instance to know value of a,b,c,d. What you should be doing in your code behind is
C#
protected void Button1_Click(object sender, EventArgs e)
{
    Class1 obj = new Class1();

    obj.EmpId = Convert.ToInt32(txtEmpId.Text);
    obj.EmpName = txtEmpName.Text;
    obj.Salary = Convert.ToInt32(txtSalary.Text);
    obj.City = txtCity.Text;
    obj.Save();
}

And change your Class1 as
C#
using System.Data.SqlClient;
 
public class Class1
{
    private int _Event;
    private int _EmpId;
    private string _EmpName;
    private int _Salary;
    private string _City;
 
    public int Event
    {
        get { return _Event; }
        set { Event = value; }
    }
 
    public int EmpId
    {
        get { return _EmpId; }
        set { EmpId = value; }
    }
 
    public string EmpName
    {
        get { return _EmpName; }
        set { EmpName = value; }
    }
 
    public int Salary
    {
        get { return _Salary; }
        set { Salary = value; }
    }
 
    public string City
    {
        get { return _City; }
        set { City = value; }
    }
 
    public void Save()
    {
        SqlConnection connect = new SqlConnection();
        SqlCommand command = new SqlCommand("YOUR INSERT QUERY GOES HERE");
        command.Parameters.Add("@EmpId", SqlDbType.Int).Value = EmpId;
        command.Parameters.Add("@Emp_name", SqlDbType.NVarChar).Value = EmpName;
        command.Parameters.Add("@Salary", SqlDbType.Int).Value = Salary;
        command.Parameters.Add("@City", SqlDbType.NVarChar).Value = City;
        connect.Open();
        command.ExecuteNonQuery();
        command.Connection.Close();
    }
}

Regards,
 
Share this answer
 
v2
Comments
Rambo_Raja 7-Jun-13 8:17am    
still records are not entering in database. my sp is as follows:
ALTER procedure [dbo].[spAllinone]
(
@event tinyint,
@Empid int = null,
@Empname nvarchar(50)=null,
@Salary int=null,
@City nvarchar(50)=null
)

as
if @event = 1
begin
insert into employee_tab(Empname,Salary,City)
values (@Empname,@Salary,@City)
end

else if @event = 2
begin
update employee_tab
set Empname = @Empname,Salary = @Salary,City = @City
where (Empid = @Empid)
End

else if @event = 3
begin
delete from employee_tab
where (Empid = @Empid)
End
.so when in form i enter event 1 then insert should happen and so on but none is working. in code behind file i have also changed my sp name.but nothing is working please-2 help me.
Prasad Khandekar 7-Jun-13 8:43am    
Add following statement
command.Parameters.Add("@event", SqlDbType.Int).Value = 1;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900