Click here to Skip to main content
15,904,287 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have created a stored procedure named storedproceed. the code i done in class file is as :
C#
public class Class1
{
    private int _Event;
    private int _EmpId;
    private string _EmpName;
    private int _Salary;
    private string _City;
    private int _Atype;

    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; }
    }
	public void save()
	{
        String constr = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
        SqlConnection connect = new SqlConnection(constr);
        SqlCommand command = new SqlCommand("spAllinone", 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();

the code in my .cs file i as :
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.Atype = Convert.ToInt32(txtEvent.Text);
    obj.save();

what the problem is when i debug my program it gives error that stored procedure is not found.why?even i check it is present in database.
Posted
v2
Comments
kumar2413 10-Jun-13 1:58am    
Try to put the connect.Open() after connection string statement.
What is your connectionstring?
Rambo_Raja 10-Jun-13 2:12am    
add name="TestConnectionString" connectionString="Data Source=.\SQLEXPRESS;Integrated Security=True;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient"
I guess its wrong. Where is the DataBase name here?

Please follow the appropriate connection string here - http://connectionstrings.com/.

Looks like you are not passing in the right SP name in the line - SqlCommand command = new SqlCommand("spAllinone", connect);
 
Share this answer
 
Comments
Do you have any connectionstring declared with name "constr" in web.config?
As I said the TestConnectionString is not correct, so look at the below website and correct it.
http://connectionstrings.com/.

Also, the line...

SqlConnection connect = new SqlConnection("constr");

is wrong, if you don't have a string named as "constr" in web.config. Instead write like below...

SqlConnection connect = new SqlConnection(constr);

as you hve one constr declared to read the TestConnectionString.
Nelek 10-Jun-13 4:20am    
Please accept a suggestion. If you have to add code to answer someone or to clarify something of your question. Please use the widget "Improve question" and add it there, you can format the code correctly and is much easier to read than in a comment.
Declare it as Global, else you can't access.
The Connection string is wrong. Where is the DataBase name here? Specify that.

Please follow the appropriate connection string from here - http://connectionstrings.com/.
 
Share this answer
 

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