标题:窗体数据修改帮我改出代码错误,并能运行!谢谢!!
只看楼主
噼里啪啦物
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-10-23
结帖率:100%
已结贴  问题点数:10 回复次数:4 
窗体数据修改帮我改出代码错误,并能运行!谢谢!!
private void DataBind(int currentPosition)
        {
            if (currentPosition == 0)
            {
                buttonFirst.Enabled = false;
                buttonPrevious.Enabled = false;
                buttonNext.Enabled = true;
                buttonLast.Enabled = true;
            }
            else
            {
                if (currentPosition == comboBoxNumber.Items.Count - 1)
                {
                    buttonFirst.Enabled = true;
                    buttonPrevious.Enabled = true;
                    buttonNext.Enabled = false;
                    buttonLast.Enabled = false;
                }
                else
                {
                    buttonFirst.Enabled = true;
                    buttonPrevious.Enabled = true;
                    buttonNext.Enabled = true;
                    buttonLast.Enabled = true;
                }

            }
            BindingContext[xsglDataSet, "xsxxb"].Position = currentPosition;
            string exePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string fileName = exePath + xsglDataSet.Tables["xsxxb"].Rows[currentPosition]["相片URL"].ToString ();
            FileInfo test = new FileInfo(fileName);
            if (test.Exists)
            {
                Image image1 = Image.FromFile(fileName);
                pictureBox1.Image = image1;
            }
            else
            {
                pictureBox1.Image = null;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            int n=xsxxbTableAdapter.Fill (xsglDataSet.xsxxb );
           comboBoxNumber.SelectedIndex=0;
                DataBind(0);
           buttonModiOrcancel.Text="修改数据";
            buttonBrowse.Enabled=false;
           buttonUpdate.Enabled=false;
           textBoxName.ReadOnly=true;
             textBoxSex.ReadOnly=true;
          textBoxClass.ReadOnly=true;
          textBoxBirthday.ReadOnly=true;
           textBoxFace.ReadOnly=true;
            textBoxNation.ReadOnly=true;
            textBoxPlace.ReadOnly=true;
              textBoxState.ReadOnly=true;

            // TODO: 这行代码将数据加载到表“xsglDataSet.xsxxb”中。您可以根据需要移动或移除它。
              this.xsxxbTableAdapter.Fill(this.xsglDataSet.xsxxb);

        }

        private void comboBoxNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            {
                int current = comboBoxNumber.SelectedIndex;
                DataBind(current);
            }
        }

        private void buttonFirst_Click(object sender, EventArgs e)
        {
            comboBoxNumber.SelectedIndex = 0;
            DataBind(0);

        }

        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            int current = comboBoxNumber.SelectedIndex - 1;
            comboBoxNumber.SelectedIndex = current;
            DataBind(current);
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            int current = + 1;
            comboBoxNumber.SelectedIndex = current;
            DataBind(current);
        }

        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "图像文件(*.bmp)|*.bmp";
            DialogResult dlr = ofd.ShowDialog();
            if (dlr == DialogResult.OK)
            {
                Image image1 = Image.FromFile(ofd.FileName);
                textBoxPhotoPath.Text = ofd.FileName;
                pictureBox1.Image = image1;
            }
        }



        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void buttonModiOrcancel_Click(object sender, EventArgs e)
        {

                        if (buttonModiOrcancel.Text == " 修改数据")
                        {

                            buttonModiOrcancel.Text = "取消修改";
                            textBoxName.ReadOnly = false;
                            textBoxSex.ReadOnly = false;
                            textBoxClass.ReadOnly = false;
                            textBoxBirthday.ReadOnly = false;
                            textBoxFace.ReadOnly = false;
                            textBoxNation.ReadOnly = false;
                            textBoxPlace.ReadOnly = false;
                            textBoxState.ReadOnly = false;
                            textBoxName.Focus();
                            buttonBrowse.Enabled = true;
                            buttonUpdate.Enabled = true;
                            panel1.Enabled = false;
                        }
                        else
                        {
                            int n = comboBoxNumber.SelectedIndex;
                            xsglDataSet.Clear();
                            xsxxbTableAdapter.Fill ( this.xsglDataSet .xsxxb );
                            comboBoxNumber.SelectedIndex = n;
                            BindingContext[xsglDataSet, "xsxxb"].Position = n;
                            DataBind(n);
                            buttonModiOrcancel.Text = " 修改数据";
                            textBoxName.ReadOnly = true;
                            textBoxSex.ReadOnly = true;
                            textBoxClass.ReadOnly = true;
                            textBoxBirthday.ReadOnly = true;
                            textBoxFace.ReadOnly = true;
                            textBoxNation.ReadOnly = true;
                            textBoxPlace.ReadOnly = true;
                            textBoxState.ReadOnly = true;
                            buttonBrowse.Enabled = false;
                            buttonUpdate.Enabled = false;
                            panel1.Enabled = true;

                        }

                    }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            string number = comboBoxNumber.Text;
            string name = textBoxName.Text;
            string sex = textBoxSex.Text;
            string Class = textBoxClass.Text;
            string birthday = textBoxBirthday.Text;
            string face = textBoxFace.Text;
            string nation = textBoxNation.Text;
            string Place = textBoxPlace.Text;

            string studystate = textBoxState.Text;
            string exepath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string photopath = textBoxPhotoPath.Text;
            int n = exepath.Length;
            int m = photopath.Length;
            if (m > n)
            {
                photopath = photopath.Substring(n, m - n);


                SqlCommand myUpdateCommand = sqlconnection1.CreateCommand();
                = "update xsxxb set  姓名=@ xm,性别=@xb,班级=@bj," + "出生日期=@csrq, 政治面貌=@zzmm,民族@mz,籍贯=@jg,学习状况=@xxzk," + "相片 URL=@txlj  where 学号='" + number + "'";
                myUpdateCommand.Parameters.Add("@xm", SqlDbType.NChar, 10, "姓名");
                myUpdateCommand.Parameters.Add("@xb", SqlDbType.NChar, 10, "性别");
                myUpdateCommand.Parameters.Add("@bj", SqlDbType.NChar, 10, "班级");
                myUpdateCommand.Parameters.Add("@csrq", SqlDbType.NChar, 10, "出生日期");
                myUpdateCommand.Parameters.Add("@zzmm", SqlDbType.NChar, 10, "政治面貌");
                myUpdateCommand.Parameters.Add("@mz", SqlDbType.NChar, 10, "民族");
                myUpdateCommand.Parameters.Add("@jg", SqlDbType.NChar, 10, "籍贯");
                myUpdateCommand.Parameters.Add("@xxzk", SqlDbType.NChar, 10, "学习状况");
                myUpdateCommand.Parameters.Add("@txlj", SqlDbType.NChar, 10, "相片URL");
                myUpdateCommand.Parameters["@ xm"].Value = name;
                myUpdateCommand.Parameters["@ xb"].Value = sex;
                myUpdateCommand.Parameters["@ bj"].Value = Class;
                myUpdateCommand.Parameters["@ csrq"].Value = birthday;
                myUpdateCommand.Parameters["@ zzmm"].Value = face;
                myUpdateCommand.Parameters["@ mz"].Value = nation;
                myUpdateCommand.Parameters["@ jg"].Value = Place;
                myUpdateCommand.Parameters["@ xxzk"].Value = studystate;
                myUpdateCommand.Parameters["@ txlj"].Value = photopath;
                SqlConnection1.Open();
                try
                {
                    myUpdateCommand.ExecuteNonQuery();
                    MessageBox.Show("数据更新。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception E)
                {
                    MessageBox.Show("数据格式错误,不能更新数据。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                SqlConnection1.close();
                n = comboBoxNumber.SelectedIndex;
                xsglDataSet.Clear();
                xsxxbTableAdapter.Fill(xsglDataSet.xsxxb);
                comboBoxNumber.SelectedIndex = n;
                BindingContext[xsglDataSet, "xsxxb"].Position = n;
                DataBind(n);
                buttonModiOrcancel.Text = "修改数据";
                textBoxName.ReadOnly = true;
                textBoxName.ReadOnly = true;
                textBoxSex.ReadOnly = true;
                textBoxClass.ReadOnly = true;
                textBoxBirthday.ReadOnly = true;
                textBoxFace.ReadOnly = true;
                textBoxNation.ReadOnly = true;
                textBoxPlace.ReadOnly = true;
                textBoxState.ReadOnly = true;
                buttonBrowse.Enabled = false;
                buttonUpdate.Enabled = false;
                panel1.Enabled = true;
            }

        }

        private void fillByToolStripButton_Click(object sender, EventArgs e)
        {
            try
            {
                this.xsxxbTableAdapter.FillBy(this.xsglDataSet.xsxxb);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

      
    }
}      
搜索更多相关主题的帖子: 窗体 数据 代码 运行 
2010-11-06 17:57
guang
Rank: 4
来 自:广东深圳
等 级:贵宾
威 望:13
帖 子:1414
专家分:285
注 册:2006-4-3
得分:10 
这样长篇发上来看死人,请发有错的代码及错误信息!

不相信未作牺牲竟先可拥有,只相信靠双手找到我的欲求!!
我的博客:http://liao5930.blog.
2010-11-06 18:06
噼里啪啦物
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-10-23
得分:0 

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            string number = comboBoxNumber.Text;
            string name = textBoxName.Text;
            string sex = textBoxSex.Text;
            string Class = textBoxClass.Text;
            string birthday = textBoxBirthday.Text;
            string face = textBoxFace.Text;
            string nation = textBoxNation.Text;
            string Place = textBoxPlace.Text;

            string studystate = textBoxState.Text;
            string exepath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string photopath = textBoxPhotoPath.Text;
            int n = exepath.Length;
            int m = photopath.Length;
            if (m > n)
            {
                photopath = photopath.Substring(n, m - n);


                SqlCommand myUpdateCommand = Sqlconnection1.createcommand();


                = "update xsxxb set  姓名=@ xm,性别=@xb,班级=@bj," + "出生日期=@csrq, 政治面貌=@zzmm,民族@mz,籍贯=@jg,学习状况=@xxzk," + "相片 URL=@txlj  where 学号='" + number + "'";
                myUpdateCommand.Parameters.Add("@xm", SqlDbType.NChar, 10, "姓名");
                myUpdateCommand.Parameters.Add("@xb", SqlDbType.NChar, 10, "性别");
                myUpdateCommand.Parameters.Add("@bj", SqlDbType.NChar, 10, "班级");
                myUpdateCommand.Parameters.Add("@csrq", SqlDbType.NChar, 10, "出生日期");
                myUpdateCommand.Parameters.Add("@zzmm", SqlDbType.NChar, 10, "政治面貌");
                myUpdateCommand.Parameters.Add("@mz", SqlDbType.NChar, 10, "民族");
                myUpdateCommand.Parameters.Add("@jg", SqlDbType.NChar, 10, "籍贯");
                myUpdateCommand.Parameters.Add("@xxzk", SqlDbType.NChar, 10, "学习状况");
                myUpdateCommand.Parameters.Add("@txlj", SqlDbType.NChar, 10, "相片URL");
                myUpdateCommand.Parameters["@ xm"].Value = name;
                myUpdateCommand.Parameters["@ xb"].Value = sex;
                myUpdateCommand.Parameters["@ bj"].Value = Class;
                myUpdateCommand.Parameters["@ csrq"].Value = birthday;
                myUpdateCommand.Parameters["@ zzmm"].Value = face;
                myUpdateCommand.Parameters["@ mz"].Value = nation;
                myUpdateCommand.Parameters["@ jg"].Value = Place;
                myUpdateCommand.Parameters["@ xxzk"].Value = studystate;
                myUpdateCommand.Parameters["@ txlj"].Value = photopath;
                sqlconnection1.Open();
                try
                {
                    myUpdateCommand.ExecuteNonQuery();
                    MessageBox.Show("数据更新。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception E)
                {
                    MessageBox.Show("数据格式错误,不能更新数据。", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                SqlConnection1.close();
                n = comboBoxNumber.SelectedIndex;
                xsglDataSet.Clear();
                xsxxbTableAdapter.Fill(xsglDataSet.xsxxb);
                comboBoxNumber.SelectedIndex = n;
                BindingContext[xsglDataSet, "xsxxb"].Position = n;
                DataBind(n);
                buttonModiOrcancel.Text = "修改数据";
                textBoxName.ReadOnly = true;
                textBoxName.ReadOnly = true;
                textBoxSex.ReadOnly = true;
                textBoxClass.ReadOnly = true;
                textBoxBirthday.ReadOnly = true;
                textBoxFace.ReadOnly = true;
                textBoxNation.ReadOnly = true;
                textBoxPlace.ReadOnly = true;
                textBoxState.ReadOnly = true;
                buttonBrowse.Enabled = false;
                buttonUpdate.Enabled = false;
                panel1.Enabled = true;
            }

        }
那个sqlconnection1 为不存在的名称,怎么弄啊!!
2010-11-06 18:09
小红帽
Rank: 2
等 级:论坛游民
帖 子:25
专家分:18
注 册:2010-10-28
得分:0 
       可能是你没有定义sqlconnection1,加这句话SqlConnection sqlconnection1=new SqlConnection();,这样可能就不会有错了
!
2010-11-06 20:39
c1_wangyf
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:7
帖 子:665
专家分:2832
注 册:2010-5-24
得分:0 
以下是引用小红帽在2010-11-6 20:39:07的发言:

       可能是你没有定义sqlconnection1,加这句话SqlConnection sqlconnection1=new SqlConnection();,这样可能就不会有错了
!
有道理!
2010-11-06 22:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-325186-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.130194 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved