当前位置:编程学习 > C#/ASP.NET >>

有关C#问题请教

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace HcitPos.BaseInfo
{
    public partial class FrmClassInfoSet : Form
    {
        public FrmClassInfoSet()
        {
            InitializeComponent();
        }
        private void ClearControl()
        {
            txtClassID.Text = "";
            txtClassName.Text = "";
            txtClassUnit.Text = "";
            txtStoreBaseLine.Text = "0";
            txtStoreLimit.Text = "0";
        }
        private void InsertRecords()
        {
            string classid = txtClassID.Text;
            string classname = txtClassName.Text;
            string classunit = txtClassUnit.Text;
            int storelimit = int.Parse(txtStoreBaseLine.Text);
            int storebaseline = int.Parse(txtStoreLimit.Text);

            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=HcitPos;Integrated Security=True");
            conn.Open();

            string insert = "insert into goodsclass (classid, classname, classunit, storelimit, storebaseline) values ('" + classid + "', '" + classname + "', '" + classunit + "', '" + storelimit + "', '" + storebaseline + "')";
            SqlCommand cmd = new SqlCommand(insert, conn);

            cmd.ExecuteNonQuery();

            MessageBox.Show("操作完成!");
            ClearControl();
          
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (action == SysUtility.OperationMethod.Insert)
                InsertRecords();
            if (action == SysUtility.OperationMethod.Edit)
                EditRecords();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        private SysUtility.OperationMethod action = SysUtility.OperationMethod.Insert;
        public void SetActionMode(SysUtility.OperationMethod method)
        {
            this.action = method;
            if (method == SysUtility.OperationMethod.Insert)
                this.Text = "添加商品类别";
            else
                this.Text = "编辑商品类别";
        }
        public void InitControl(string classID, string className, string classUnit, string storeLimit, string storeBaseLine)
        {
            txtClassID.Text = classID;
            txtClassName.Text = className;
            txtClassUnit.Text = classUnit;
            txtStoreLimit.Text = storeLimit;
            txtStoreBaseLine.Text = storeBaseLine;
            txtClassName.Focus();
        }
        private void EditRecords()
        {
            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=HcitPos;Integrated Security=True");
            conn.Open();

            string update = "update goodsclass set classname = '" + className + "',classunit = '" + classUnit + "', storelimit ='" + storeLimit + "',storebaseline = '" + storeBaseLine + "'where classid='" + classID + "'";
            SqlCommand cmd = new SqlCommand(update, conn);
            try
            {
                cmd.ExecuteNonQuery();
            }
            finally
            {
                    conn.Close();
            }
            MessageBox.Show("操作完成!");
            Close();
        }
       

    }
}

 

 

 

 

 

 

 

 

 

 

 

 

运行后提示错误 1 当前上下文中不存在名称 “什么什么的”(字体加粗的那几个词)

 

 

怎么解决啊?

答案:这个问题很简单,你把这几个黑色粗体字重新定义一遍(明显缺少变量)。

例如(注意黑色部分)——

 

private void EditRecords()
{

string className = xxx;

string classUnit= xxx;

//自己定义,注意类型匹配


SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=HcitPos;Integrated Security=True");
conn.Open();

string update = "update goodsclass set classname = '" + className + "',classunit = '" + classUnit + "', storelimit ='" + storeLimit + "',storebaseline = '" + storeBaseLine + "'where classid='" + classID + "'";
SqlCommand cmd = new SqlCommand(update, conn);
try
{
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
MessageBox.Show("操作完成!");
Close();
}

哦,楼上也说了,我就补充清楚点吧,你把那些变量定义到了上面的InsertRecords()函数里面,下面的函数怎么访问得到呢?
你应该定义到全局,然后在InsertRecords()里面赋值就没问题了。

前面的已经说的很详细了,EditRecords()方法中,你就没有定义这几个变量,想到 InitControl(string classID, string className, string classUnit, string storeLimit, string storeBaseLine)
方法中调用当然是不可以的啊

上一个:C#编程学习
下一个:c#编程问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,