0
namespace ClassLibrary1
{
    public class open    
    {        
        SqlCommand cmd;

        public SqlCommand Cmd
        {
            get { return cmd; }
            set { cmd = value; }
        }
        string storedp;

        public string Storedp
        {
            get { return storedp; }
            set { storedp = value; }
        }

        public open(string storedp, SqlCommand cmd)
        {
            SqlConnection con = new SqlConnection("Data Source=.; Initial Catalog=cau; Integrated Security=true");
            con.Open();
            this.cmd = cmd = new SqlCommand(this.storedp = storedp, con);
            this.Cmd.CommandType = CommandType.StoredProcedure;

        }
    }
}

That's the code which I wrote for my web page. I wrote it because I want open sqlconnection with one class which I can tell its must use which stored proc. and which sqlcommand as you see.

But problem is

protected void Page_Load(object sender, EventArgs e)
{
    open op = new open("diz", cmd);
    SqlDataReader rd = cmd.ExecuteReader();
    while (rd.Read())
    {
        drop1.Items.Add(rd.GetString(0));
    }
}

This is my ASP.net page. When I try to run my class it says "there is no property for cmd" and "cmd does not exist in current context". However i create it in my "open" class Right?

Second question: why its just says for my SqlCommand but not for my string?

Note:this error is not about adding namespace ,reference or something like that.

1
  • 3
    You should read this link C# Tutorials Commented Sep 16, 2013 at 13:21

3 Answers 3

3

Other than mistakes in your class design and database access practices, The first problem is the parameter you are passing to your constructor.

You haven't defined cmd in your Page_Load. Your call to constructor should look like:

open op = new open("diz", new SqlCommand());

(You may also look at the Naming Conventions - MSDN)

Second question: why its just says for my SqlCommand but not for my string?

There is no error because you are passing a valid string constant to your parameter storedp

You may see this old article about Best Practices for Using ADO.NET - MSDN

Sign up to request clarification or add additional context in comments.

Comments

0

Use ref in page_Load and class to cmd:

In Page_Load:

SqlCommand cmd = new SqlCommand();
open op = new open("diz",ref cmd);

In class :

public open(string storedp, ref SqlCommand cmd)
{ 
   //...

Comments

0

Apart from failing to pass a qualified parameter value for "cmd" (as Habib pointed out);

In your constructor you have this code:

this.cmd = cmd = new SqlCommand(this.storedp = storedp, con);

Does it really matter if you pass a parameter to the call? You may remove the parameter altogether.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.