使用visual studio表单将信息输入数据库

时间:2014-03-09 20:38:42

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

当我尝试将信息输入到连接到我的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,但这更多的是创建一些读取和写入数据到db的表单。我感谢任何人能提供的任何帮助,

1 个答案:

答案 0 :(得分:0)

如果这不是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  
}