0

I have the following code, this code was recommended to me by stackoverflow user on my previous post, its throwing some error

protected void Button2_Click(object sender, EventArgs e)
    {
        String a = DropDownList1.SelectedItem.Value;
        String b = DropDownList3.SelectedItem.Value.PadLeft(3, '0');      
        String c = TextBox2.Text.PadLeft(5,'0').ToString();
        String d = TextBox3.Text.ToString();
        String digit = a+ b  + c + d;
        string sql = "select * from testcase.main where reg_no =?";

try
        {
            using (OdbcConnection myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=testcase;User=root;Password=root;Option=3;"), 
                OdbcCommand cmd = new OdbcCommand(sql, myConn))
            {
                    myConn.Open();
                    //**
                    cmd.Parameters.AddWithValue("?", digit);
                    using (odbcReader MyReader = cmd.ExecuteReader())
                    {
                        //**
                        while (MyReader.Read())
                        {
                            String f = MyReader["pet_name"].ToString();
                            String g = MyReader["res_name"].ToString();

                            Label9.Visible = true;
                            Label9.Text = f;

                            Label10.Visible = true;
                            Label10.Text = "VS";

                            //Label11.Visible = true;
                            Label11.Text = g;

                        }
                    }
                }
            }

        catch (Exception e1)
        {
            Response.Write(e1.ToString());
        }
    }

the error is:

Error 1 Cannot use more than one type in a for, using, fixed, or declaration statement 

How can i resolve this error??what is the problem in declaration?

1
  • Yes i understood also but dint know the solution Commented Nov 18, 2010 at 5:47

4 Answers 4

1

You have two variables wrapped in a using statement:

using (OdbcConnection myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=testcase;User=root;Password=root;Option=3;"), 
                OdbcCommand cmd = new OdbcCommand(sql, myConn))

Separate them into two nested using statements.

using( IDisposable obj1 )
{
    using( IDisposable obj2 )
    {
         // code
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

You don't need double-indentation.
using (OdbcCommand cmd = new OdbcCommand(sql, myConn)) i seperated this way and now i got another error
ERRIR: The type or namespace name 'odbcReader' could not be found (are you missing a using directive or an assembly reference?)
@SLaks: I don't think you never 'need' indentation in C#. I think you mean that you don't need an extra block.
@Ishan, have you checked to see if you have the correct using directives? (as the new error suggests)
|
1

Have you tried it without having both myConn and cmd in this line:

        using (OdbcConnection myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=testcase;User=root;Password=root;Option=3;"), 
            OdbcCommand cmd = new OdbcCommand(sql, myConn))

Comments

1

I believe you had some typos. Please try this:

protected void Button2_Click(object sender, EventArgs e)
{
    String a = DropDownList1.SelectedItem.Value;
    String b = DropDownList3.SelectedItem.Value.PadLeft(3, '0');      
    String c = TextBox2.Text.PadLeft(5,'0').ToString();
    String d = TextBox3.Text.ToString();
    String digit = a+ b  + c + d;
    string sql = "select * from testcase.main where reg_no =?";

    try
    {
        using (OdbcConnection myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=testcase;User=root;Password=root;Option=3;"))
        using (OdbcCommand cmd = new OdbcCommand(sql, myConn))
        {
            myConn.Open();
            //**
            cmd.Parameters.AddWithValue("?", digit);
            using (OdbcDataReader MyReader = cmd.ExecuteReader())
            {
                //**
                while (MyReader.Read())
                {
                    String f = MyReader["pet_name"].ToString();
                    String g = MyReader["res_name"].ToString();

                    Label9.Visible = true;
                    Label9.Text = f;

                    Label10.Visible = true;
                    Label10.Text = "VS";

                    //Label11.Visible = true;
                    Label11.Text = g;

                }
            }
        }
    }
    catch (Exception e1)
    {
        Response.Write(e1.ToString());
    }
}

1 Comment

odbcReader is not a known type. OdbcDataReader should be. Also you can stack your using statements if nesting them is too hard to read.
1

You cannot declare variables of two different types in a single using statement.

You need to declare the OleDbConnection and OleDbCommand in two different using statements:

using (OdbcConnection myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=testcase;User=root;Password=root;Option=3;"))
using (OdbcCommand cmd = new OdbcCommand(sql, myConn)) {
    ...
}

1 Comment

Thank you.I did that but i am getting another error ERROR 1 The type or namespace name 'odbcReader' could not be found (are you missing a using directive or an assembly reference?)

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.