我试图从数据库中获取值,但得到的错误是cmd、dr在当前上下文中不存在
本文关键字:cmd 错误 dr 上下文 不存在 数据库 获取 | 更新日期: 2023-09-27 17:59:24
可能重复:
C#在获取时出现一些错误
public partial class BioreCottonPurchaseSlip2011 : Form
{
Library obj = new Library();
public BioreCottonPurchaseSlip2011()
{
InitializeComponent();
}
public void enableDisableControls(bool flag)
{
TxtExtensionNo.Enabled = flag;
TxtFarmerCode.Enabled = flag;
TxtFarmerName.Enabled = flag;
TxtBasicPrice.Enabled = flag;
TxtPremium.Enabled = flag;
TxtWeight.Enabled = flag;
TxtTotalAmountBasic.Enabled = flag;
TxtTotalAmountPremium.Enabled = flag;
TxtBalancePay.Enabled = flag;
BtnSave.Enabled = flag;
BtnCancel.Enabled = flag;
}
public void clearControls()
{
TxtExtensionNo.Text = "";
TxtFarmerCode.Text = "";
TxtFarmerName.Text = "";
TxtBasicPrice.Text = "";
TxtPremium.Text = "";
TxtWeight.Text = "";
TxtTotalAmountBasic.Text = "";
TxtTotalAmountPremium.Text = "";
TxtBalancePay.Text = "";
}
private void BtnNew_Click(object sender, EventArgs e)
{
if (obj.GetConnection() == true)
{
lblError.Text = "Connected !!!";
}
else
{
lblError.Text = "Not connnected !!!";
}
enableDisableControls(true);
BtnNew.Enabled = false;
// lblError.Text = "";
string connectionString = "Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True";
string sql = "SELECT * FROM cottonpurchase";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
da.Fill(ds, "cottonpurchase");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "cottonpurchase";
}
private void BtnSave_Click(object sender, EventArgs e)
{
if (obj.GetConnection() == true)
{
//dateTimePicker1.Value = DateTime.Today;
MessageBox.Show("insert into cottonpurchase values(" + TxtExtensionNo.Text + ",'" + monthCalendar1.TodayDate + "'," + TxtFarmerCode.Text + ",'" + TxtFarmerName.Text + "'," + TxtBasicPrice.Text + "," + TxtPremium.Text + "," + TxtWeight.Text + "," + TxtTotalAmountBasic.Text + "," + TxtTotalAmountPremium.Text + "," + TxtBalancePay.Text + ")");
if (obj.ExecuteSQLStatement("insert into cottonpurchase values(" + TxtExtensionNo.Text + ",'" + monthCalendar1.TodayDate + "'," + TxtFarmerCode.Text + ",'" + TxtFarmerName.Text + "'," + TxtBasicPrice.Text + "," + TxtPremium.Text + "," + TxtWeight.Text + "," + TxtTotalAmountBasic.Text + "," + TxtTotalAmountPremium.Text + "," + TxtBalancePay.Text + " )") == true)
{
lblError.Text = "Item(s) Saved";
clearControls();
BtnSave.Enabled = false;
BtnNew.Enabled = true;
enableDisableControls(false);
}
else
{
lblError.Text = "Item(s) Not Saved";
}
}
else
{
lblError.Text = "Connection Error. Please contact your administrator.";
}
enableDisableControls(false);
clearControls();
BtnNew.Enabled = true;
try
{
//double getvat = 0;
//double calculatevat = 0;
//getvat = Convert.ToDouble(TxtTotalAmountBasic.Text);
//calculatevat = getvat * 0.18;
//TxtBasicPrice.Text = calculatevat.ToString();
//TxtBasicPrice.Enabled = false;
}
catch (Exception)
{
// lblError.Text = "Please contact your administrator. (Error - TARS0001DATMIS)";
clearControls();
enableDisableControls(false);
BtnNew.Enabled = true;
//lblInvoiceNo.Text = "INVOIC NO";
}
}
private void BtnCancel_Click(object sender, EventArgs e)
{
enableDisableControls(false);
clearControls();
BtnNew.Enabled = true;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void TxtFarmerCode_TextChanged(object sender, EventArgs e)
{
try
{
SqlConnection conn= new SqlConnection("Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True");
conn.Open();
cmd = new SqlCommand("Select farmername, from cottonpurchase where farmercode=@aa", conn);
cmd.Parameters.Add("@aa", SqlDbType.Int).Value = TxtFarmerCode.Text;
dr = cmd.ExecuteReader();
if (dr.HasRows == false)
{
throw new Exception();
}
if (dr.Read())
{
// textBox1.Text = dr[0].ToString(); Since U r going to give the ID and retrieve in textBox1.
TxtFarmerName.Text = dr[0].ToString();
//textBox3.Text = dr[1].ToString();
//textBox4.Text = dr[2].ToString();
//textBox7.Text = dr[3].ToString();
//dateTimePicker1.Text = dr[4].ToString();
//dateTimePicker2.Text = dr[5].ToString();
//textBox5.Text = dr[6].ToString();
}
}
catch
{
// lblError = "THE GIVEN ID IS UNAVAILABLE";
}
finally
{
conn.Close();
}
}
}
您必须定义变量类型
SqlCommand cmd = new SqlCommand(...);
SqlReader dr = cmd.ExecuteReader();
在TxtFarmerCode_TextChanged
中,您尚未定义变量cmd
或dr
。
你会想要这样的东西:
SqlCommand cmd = new SqlCommand("Select farmername, from cottonpurchase where farmercode=@aa", conn);
[...]
SqlDataReader dr = cmd.ExecuteReader();