【问题标题】:Entering information into database with visual studio form使用 Visual Studio 表单将信息输入数据库
【发布时间】:2014-03-09 20:38:41
【问题描述】:

当我尝试将信息输入连接到我的 Visual Studio 2012 表单的数据库时遇到问题。当我开始调试时,如果我在字段中输入信息并点击提交,则没有任何内容输入到我的数据库中。如果我再次输入信息并点击提交,则信息将写入我的数据库。我不是很擅长编程,所以我使用数据源来链接我的 Visual Studio 表单和我的数据库。

这就是我的表单的样子; data form before entry

这是我输入数据后的表单; data form with information

这就是点击提交时发生的情况; confirmation message

这是第一次点击提交后的数据库; db entries after first submission

这就是我再次输入信息时发生的情况; db entries after second submission

在此之后的任何条目都将正确输入,并且一切都很好。我很困惑为什么信息(除了空条目)直到我第二次单击提交时才会输入。虽然它“在技术上有效”,但它并不完全正确。对此问题的任何帮助将不胜感激。

这是我在新客户表单中提交按钮的代码;

namespace WindowsFormsApplication2
{
public partial class frmNewCustomer : Form
{
    public frmNewCustomer()
    {
        InitializeComponent();
    }

    private void btnAddCustomer_Click(object sender, EventArgs e)
    {
        string cstFName;
        string cstLName;
        string cstAddress;
        string cstCity;
        string cstState;
        string cstZip;
        string cstPhone;
        string cstEmail;

        cstFName = cstFNameTxtBox.Text;
        cstLName = cstLNameTxtBox.Text;
        cstAddress = cstAddressTxtBox.Text;
        cstCity = cstCityTxtBox.Text;
        cstState = cstStateTxtBox.Text;
        cstZip = cstZipTxtBox.Text;
        cstPhone = cstPhoneTxtBox.Text;
        cstEmail = cstEmailTxtBox.Text;

        // exception handler for empty fields    
        if (cstFName == "")
        {
            MessageBox.Show("Please enter your first name!");
        }
        else if (cstLName == "")
        {
            MessageBox.Show("Please enter your last name!");
        }
        else if (cstAddress == "")
        {
            MessageBox.Show("Please enter your address!");
        }
        else if (cstCity == "")
        {
            MessageBox.Show("Please enter your city!");
        }
        else if (cstState == "")
        {
            MessageBox.Show("Please enter your state!");
        }
        else if (cstZip == "")
        {
            MessageBox.Show("Please enter your zip!");
        }
        else if (cstPhone == "")
        {
            MessageBox.Show("Please enter your Phone!");
        }
        else if (cstEmail == "")
        {
            MessageBox.Show("Please enter your email!");
        }
        else
        {
            MessageBox.Show("First Name: " + cstFName + " Last Name: " + cstLName + "\r\n" +
                            "Address: " + cstAddress + "\r\n" +
                            "City: " + cstCity + " State: " + cstState + " Zip: " + cstZip + "\r\n" +
                            "Phone: " + cstPhone + " Email: " + cstEmail + "\r\n" + "\r\n" +
                            "Has been added to the database.");
        }

        this.tblCustomersBindingSource.AddNew();
        this.tblCustomersBindingSource.EndEdit();
        this.tblCustomersTableAdapter.Update(this.dataSet1.tblCustomers); // Updating the DB Table
    }

    private void frmNewCustomer_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'dataSet1.tblCustomers' table. You can move, or remove it, as needed.  Loads it into start of form!
        // this.tblCustomersTableAdapter.Fill(this.dataSet1.tblCustomers);

    }
  }
}

我意识到我的异常处理充其量是 janky,但这更多是关于创建一些将数据读取和写入数据库的表单。我感谢任何人能够提供的任何帮助,

【问题讨论】:

    标签: c# mysql winforms visual-studio-2012


    【解决方案1】:

    如果这不是 winforms(不确定是否重要),典型的流程是这样的:

    Create a connection.  
    Write your insert statement.  
    Do data validation.  
    Submit to database.  
    Commit transaction.  
    

    试试这个:

    bool shouldSubmit = false;
         // exception handler for empty fields    
                if (cstFName == "")
                {
                    MessageBox.Show("Please enter your first name!");
                }
                else if (cstLName == "")
                {
                    MessageBox.Show("Please enter your last name!");
                }
                else if (cstAddress == "")
                {
                    MessageBox.Show("Please enter your address!");
                }
                else if (cstCity == "")
                {
                    MessageBox.Show("Please enter your city!");
                }
                else if (cstState == "")
                {
                    MessageBox.Show("Please enter your state!");
                }
                else if (cstZip == "")
                {
                    MessageBox.Show("Please enter your zip!");
                }
                else if (cstPhone == "")
                {
                    MessageBox.Show("Please enter your Phone!");
                }
                else if (cstEmail == "")
                {
                    MessageBox.Show("Please enter your email!");
                }
                else
                {
                    shouldSubmit = true;
                    MessageBox.Show("First Name: " + cstFName + " Last Name: " + cstLName + "\r\n" +
                                    "Address: " + cstAddress + "\r\n" +
                                    "City: " + cstCity + " State: " + cstState + " Zip: " + cstZip + "\r\n" +
                                    "Phone: " + cstPhone + " Email: " + cstEmail + "\r\n" + "\r\n" +
                                    "Has been added to the database.");
                }
    
    if(shouldSubmit)
    {
                this.tblCustomersBindingSource.AddNew();
                this.tblCustomersBindingSource.EndEdit();
                this.tblCustomersTableAdapter.Update(this.dataSet1.tblCustomers); // Updating the DB Table  
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 2021-09-30
      • 2015-09-28
      • 1970-01-01
      • 2013-09-05
      相关资源
      最近更新 更多