【问题标题】:Save Records to DataBase using C# sql in windows form datagrid在 Windows 表单数据网格中使用 C# sql 将记录保存到数据库
【发布时间】:2014-09-25 13:46:11
【问题描述】:

当我使用 C# sql windows form Datagrid 将记录保存到数据库时遇到问题,当我重新启动应用程序或捕获异常错误时,我丢失了所有记录,但这个问题并不经常发生,但是当有异常错误我的所有记录都丢失了

这是我的保存按钮代码。

private void btnSave_Click(object sender, EventArgs e)
        {

            try
            {
                //Reconnect to Database
                System.Data.SqlClient.SqlCommandBuilder cb;
                cb = new System.Data.SqlClient.SqlCommandBuilder(dataAdapter);
                // cb.DataAdapter.Update(ds.Tables["Impi"]);

                //Create a new row
                DataRow dr = ds.Tables["tblImpi"].NewRow();


                //save the record to first column
                dr[0] = txtSerial.Text;

                //Check data entry if its no empt
                if (txtName.Text != "")
                {

                    dr[1] = txtName.Text;

                }

                //Check if combox has values and assign one the values to it
                if (cbModel.Text == "MKI" || cbModel.Text == "MKII")
                {
                    dr[2] = cbModel.Text;
                }

                if (cbStatus.Text == "Serviceble" || cbStatus.Text == "Unserviceble")
                {
                    dr[3] = cbStatus.Text;
                }

                if (cbDeprtmnt.Text == "AIR" || cbDeprtmnt.Text == "LAND" || cbDeprtmnt.Text == "NAVY" || cbDeprtmnt.Text == "SPECIAL FORCE")
                {


                    dr[4] = cbDeprtmnt.Text;
                }

                if (cmbVesrion.Text == "10" || cmbVesrion.Text == "11" || cmbVesrion.Text == "12")
                {
                    dr[5] = cmbVesrion.Text;
                }



                if (txtGsm.Text.Length != 0)
                {
                    dr[6] = txtGsm.Text;
                }

                if (txtImei.Text.Length != 0)
                {
                    dr[7] = txtImei.Text;
                }
                //Check if serial number value is not zero
                if ((txtSerial.Text.Length != 0) || (txtGsm.Text.Length!=0) || (txtImei.Text.Length!=0))
                {
                    //Create a search Method to search before you save the serial number
                    bool search = SearchRecBeforeSave(txtSerial.Text, txtName.Text,txtGsm.Text,txtImei.Text);
                    //bool search = SearchSerialNumberBeforeSave(txtSerial.Text);
                    if (search == false)
                    {
                        // DialogResult dr2 = MessageBox.Show("Are you sure  you want to save this serial number", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (txtGsm.Text == "")
                        {
                           txtGsm.Text = "";
                            txtGsm.ForeColor = Color.Red;
                            tootip.SetToolTip(txtGsm, txtGsm.Text);

                        }

                        if (txtImei.Text == "")
                        {
                            txtImei.Text = " ";
                            txtImei.ForeColor = Color.Red;
                            tootip.SetToolTip(txtImei, txtImei.Text);

                        }

                        if (txtName.Text.Length == 0)
                        {

                            txtName.Text = "";
                            txtName.ForeColor = Color.Red;
                            tootip.SetToolTip(txtName, txtName.Text);
                        }

                        else if (txtGsm.Text != "" && txtImei.Text != "" && txtName.Text.Length != 0)
                        {


                            DialogResult dr2 = MessageBox.Show("Are you sure  you want to save this serial number", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (dr2 == DialogResult.Yes)
                            {
                                ds.Tables["tblImpi"].Rows.Add(dr);
                                System.Data.SqlClient.SqlCommandBuilder cb1;
                                cb1 = new System.Data.SqlClient.SqlCommandBuilder(dataAdapter);
                                cb1.DataAdapter.Update(ds.Tables["tblImpi"]);
                                // dataAdapter.Update(ds,"Impi");
                               // maxRow = maxRow + 1;
                              //  inc = maxRow - 1;


                                MessageBox.Show("Serial Number Added Successful");
                            }


                        }
                        else
                        {
                            //Fix this part
                            MessageBox.Show("This Serial Number Exist and will create the duplicate.\nSerial Number not Saved");
                            MessageBox.Show("Data Entry was not saved", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show("The information that you have enterd already exist\nPlease Enter a Correct Entries ", "Data Entry");
                        txtSerial.Text = "Please Enter Impi Serial Number";
                        txtSerial.ForeColor = Color.Red;
                        tootip.SetToolTip(txtSerial, txtSerial.Text);

                        txtName.Text = "Please Enter the Track Number";
                            txtName.ForeColor = Color.Red;
                            tootip.SetToolTip(txtName, txtName.Text);


                        txtGsm.Text = "Please Enter the GSM Sim Number";
                            txtGsm.ForeColor = Color.Red;
                            tootip.SetToolTip(txtGsm, txtGsm.Text);

                        txtImei.Text = "Please Enter the IMEI Number ";
                            txtImei.ForeColor = Color.Red;

                            tootip.SetToolTip(txtImei, txtImei.Text);
                        }
                    }


                }


            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
                //MessageBox.Show("Incorrect Data Entry", "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
            }

            con.Close();

        }

【问题讨论】:

    标签: c# sql datagrid


    【解决方案1】:

    这是我提出的问题的答案,我设法解决了它

    #region Save the Records to the DataBase
    
            private void btnSave_Click(object sender, EventArgs e)
            {
    
                try
                {
                    //Reconnect to Database
                    System.Data.SqlClient.SqlCommandBuilder cb;
                    cb = new System.Data.SqlClient.SqlCommandBuilder(dataAdapter);
    
                    //Create a new row
                    DataRow dr = ds.Tables["tblImpi"].NewRow();
    
                    //save the record to first column
                    dr[0] = txtSerial.Text;
    
                    //Check data entry if its not empty
                    if (txtcallSign.Text != "")
                    {
    
                        dr[1] = txtcallSign.Text;
    
                    }
    
                    //Check if ip textbox is empty
                    if (txtIp.Text != "")
                    {
                        dr[2] = txtIp.Text;
                    }
    
                    //Check if port is empty
                    if (txtPort.Text != "")
                    {
                        dr[3] = txtPort.Text;
                    }
    
                    //check if gsm txtbox is empty
                    if (txtGsm.Text != "")
                    {
    
    
                        dr[4] = txtGsm.Text;
                    }
    
                    //check iriduim txtbox
                    if (txtiriduim.Text != "")
                    {
                        dr[5] = txtiriduim.Text;
                    }
    
    
                    //check software version
                    if (txtSoftVersion.Text != "")
                    {
                        dr[6] = txtSoftVersion.Text;
                    }
    
    
                    //Check if serial number,gsm sim,iriduim value is not zero
                    if ((txtSerial.Text.Length != 0) || (txtGsm.Text.Length != 0) || (txtiriduim.Text.Length != 0))
                    {
                        //Create a search Method to search before you save the serial number
                        bool search = SearchRecBeforeSave(txtSerial.Text, txtGsm.Text, txtiriduim.Text);
                        //bool search = SearchSerialNumberBeforeSave(txtSerial.Text);
                        if (search == false)
                        {
    
                            DialogResult dr2 = MessageBox.Show("Do you want to  save", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (dr2 == DialogResult.Yes)
                            {
                                ds.Tables["tblImpi"].Rows.Add(dr);
                                System.Data.SqlClient.SqlCommandBuilder cb1;
                                cb1 = new System.Data.SqlClient.SqlCommandBuilder(dataAdapter);
                                cb1.DataAdapter.Update(ds.Tables["tblImpi"]);
    
    
    
                                MessageBox.Show("Data Saved","Save");
                            }
    
    
    
                            // DialogResult dr2 = MessageBox.Show("Are you sure  you want to save this serial number", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            else if (txtGsm.Text == "")
                            {
                                txtGsm.Text = "";
                                txtGsm.ForeColor = Color.Red;
                                tootip.SetToolTip(txtGsm, txtGsm.Text);
    
                            }
    
                            else if (txtiriduim.Text == "")
                            {
                                txtiriduim.Text = " ";
                                txtiriduim.ForeColor = Color.Red;
                                tootip.SetToolTip(txtiriduim, txtiriduim.Text);
    
                            }
    
                            else if (txtcallSign.Text.Length == 0)
                            {
    
                                txtcallSign.Text = "";
                                txtcallSign.ForeColor = Color.Red;
                                tootip.SetToolTip(txtcallSign, txtcallSign.Text);
                            }
    
    
                        }
    
                        else
                        {
                            MessageBox.Show("The information that you have enterd already exist\nPlease Enter a Correct Entries ", "Data Entry");
                            txtSerial.Text = "Please Enter Impi Serial Number";
                            txtSerial.ForeColor = Color.Red;
                            tootip.SetToolTip(txtSerial, txtSerial.Text);
    
                            txtcallSign.Text = "Please Enter Call Sign";
                            txtcallSign.ForeColor = Color.Red;
                            tootip.SetToolTip(txtcallSign, txtcallSign.Text);
    
    
                            txtIp.Text = "Please Enter Ip Adress";
                            txtIp.ForeColor = Color.Red;
                            tootip.SetToolTip(txtIp, txtIp.Text);
    
                            txtPort.Text = "Please Enter Port Number";
                            txtPort.ForeColor = Color.Red;
                            tootip.SetToolTip(txtPort, txtPort.Text);
    
                            txtGsm.Text = "Please Enter the GSM Sim Number";
                            txtGsm.ForeColor = Color.Red;
                            tootip.SetToolTip(txtGsm, txtGsm.Text);
    
                            txtiriduim.Text = "Please Enter the IMEI Number ";
                            txtiriduim.ForeColor = Color.Red;
    
                            tootip.SetToolTip(txtiriduim, txtiriduim.Text);
                        }
                    }
    
                    else if ((txtSerial.Text == "") && (txtIp.Text == "") && (txtPort.Text == "") && (txtiriduim.Text == "") && txtGsm.Text == "")
                    {
                        MessageBox.Show("Please Enter a data", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    }
    
                }
    
    
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                    //MessageBox.Show("Incorrect Data Entry", "Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
                }
    
                con.Close();
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      相关资源
      最近更新 更多