Error in asp.net c# code
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();
开发者_如何学Python //**
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?
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
}
}
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))
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());
}
}
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)) {
...
}
精彩评论