Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the following code is not working what is wrong with it:
public class Class1
{
    private int _Event;
    private int _EmpId;
    private string _EmpName;
    private int _Salary;
    private string _City;
    private int _Atype;
    private static readonly string _connectionstring;
    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 int Atype
    {
        get { return _Atype; }
        set { _Atype = value; }
    }

    string ss = "";
    public void open()
    {
        String constr = ConfigurationManager.ConnectionStrings["testing"].ConnectionString;
        SqlConnection connect = new SqlConnection(constr);
        
    }
         
   	public void save()
	{

        
        SqlCommand command = new SqlCommand("spdatabaseconnect",connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@event", SqlDbType.TinyInt).Value = Atype;
        command.Parameters.Add("@EmpId", SqlDbType.Int).Value = EmpId ;
        command.Parameters.Add("@Emp_name", SqlDbType.NVarChar,50).Value = EmpName ;
        command.Parameters.Add("@Salary", SqlDbType.Int).Value = Salary ;
        command.Parameters.Add("@City", SqlDbType.NVarChar,50).Value = City ;
        connect.Open();
        command.ExecuteNonQuery();
        command.Connection.Close();
        command.Dispose(); 
        connect.Close();
       
    }

This my class file code .
The code for button click is :
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.Atype = Convert.ToInt32(txtEvent.Text);
     obj.save();

I want to write the connection string,connection and command object to be in open().
In save method i only want to have parameters . what to do? eg if i want to open a connection when i write object.open() then the code in open method executes.
Posted
Comments
Richard MacCutchan 12-Jun-13 6:49am    
What do you mean by "not working"; please explain in proper detail.
[no name] 12-Jun-13 6:49am    
You SqlConnection is a local variable to your open method and your open method is defined as returning void. You either need to make the connection object a class level variable or return it from the open method.
Member 9956178 12-Jun-13 6:50am    
Create a class Connect separetly for Connect
like this
ublic SqlConnection con;
public Connect()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
}
public void OpenCon()
{
try
{
con.Close();
con.Open();
}
catch (Exception)
{
con.Open();
}

}
public void CloseC()
{
try
{

con.Close();

}
catch (Exception e)
{
con.Close();
}
Then for Save
do using(Sqlconnection con = new Sqlconnection(Connect.getconnectionsource()))
{
// save method is correct
}
Keeping connection class sepate will help you in many ways!
...or if you do not want this..
You need to call Open() before Sqlcommand cmd = new sqlcommand(); statement
Rambo_Raja 12-Jun-13 7:16am    
i want to write connection string,connection object,command in a new method named open().means that i dont want to write connection string,command,connection in save method.
[no name] 12-Jun-13 7:30am    
Okay so go right ahead and do that. There is absolutely nothing stopping you.

Try adding
C#
SqlConnection connect;
at the top of your class or changing your open function to return the SqlConnection
C#
public SqlConnection open()
    {
        String constr = ConfigurationManager.ConnectionStrings["testing"].ConnectionString;
        connect = new SqlConnection(constr);
        return connect;  //choose one or the other of the methods

    }
 
Share this answer
 
v3
you should merge your open() and save() methods.
try this:
C#
public void openAndSave()
    {
        String constr = ConfigurationManager.ConnectionStrings["testing"].ConnectionString;
        SqlConnection connect = new SqlConnection(constr);        
        SqlCommand command = new SqlCommand("spdatabaseconnect",connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@event", SqlDbType.TinyInt).Value = Atype;
        command.Parameters.Add("@EmpId", SqlDbType.Int).Value = EmpId ;
        command.Parameters.Add("@Emp_name", SqlDbType.NVarChar,50).Value = EmpName ;
        command.Parameters.Add("@Salary", SqlDbType.Int).Value = Salary ;
        command.Parameters.Add("@City", SqlDbType.NVarChar,50).Value = City ;
        connect.Open();
        command.ExecuteNonQuery();
        command.Connection.Close();
        command.Dispose(); 
        connect.Close();
       
    }


Also here are some other things you need to check.
1. make sure your connection string is correct.
2. make sure that the stored procedure you are calling exists

If you need to have save() and open() methods separately please refer to solution #1 and make this change in your button click
C#
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.Atype = Convert.ToInt32(txtEvent.Text);
        obj.open();
        obj.save();
 
Share this answer
 
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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