本文介绍了使用BindingNavigator和BindingSource保存数据时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 有人可以重新检查我的逻辑吗? 我正在尝试将Customers表单中的数据写入dbo。 SFDC_SQL_MDI数据库中的tblCustomers表无法正常工作。 在frmCustomer_Load事件中,我填充了Customers和Countries数据表。将表的列绑定到Customers表单上的适当控件。最后,我将组合框设置为没有选择。Private Sub frmCustomers_Load(sender As Object, _ e As EventArgs) _ Handles Me.Load dtCustomers = objSQL.dt_MSSQL_Retrive_Data(strSQLCnn, "tblCustomers") dtCountries = objSQL.dt_MSSQL_Retrive_Data(strSQLCnn, "tblCountries") Call bs_Customers(dtCustomers, dtCountries) 'Set the ComboBoxes.SelectedIndex to -1 'No Selection made Country.SelectedIndex = -1 State.SelectedIndex = -1End Sub 控件绑定例程是:Private Sub bs_Customers(ByVal dtCustomers As DataTable, _ ByVal dtCountries As DataTable) 'Purpose: Binds the Form to the appropriate text ' or combo boxes on the frmCustomers form 'Parameters: dtCountries As DataTable - tblCountries in Database ' dtCustomers As DataTable - tblCustomers in Database ' dtStates As DataTable - tblStates in Database 'Returns: Nothing - Information is bound to the form 'Set BindingSources 'Me.bsCountries = New BindingSource 'Me.bsCountries.DataSource = dtCountries Me.bsCustomers = New BindingSource Me.bsCustomers.DataSource = dtCustomers 'Me.bsCity = New BindingSource 'Me.bsCity.DataSource = dtCity 'Have the controls been data bound? If Customer.DataBindings.Count > 0 Then Exit Sub Else 'Binding the controls to the appropate DataColumn(Field) 'Deactivated because the User does not need to see it 'txtCustID.DataBindings.Add() _ ' ("Text", Me.bsCustomers, "CustID") Customer.DataBindings.Add _ ("Text", Me.bsCustomers, "CustName", True) Identifier.DataBindings.Add _ ("Text", Me.bsCustomers, "Identifier", True) Address1.DataBindings.Add _ ("Text", Me.bsCustomers, "Address_1", True) Address2.DataBindings.Add _ ("Text", Me.bsCustomers, "Address_2", True) Country.DataBindings.Add("SelectedValue", _ Me.bsCustomers, _ "CountryID", True) 'Sets specific properties for the ComboBox Country.ValueMember = "CountryID" Country.DisplayMember = "Country" Country.DataSource = dtCountries State.DataBindings.Add("SelectedValue", Me.bsCustomers, "StateID", True) txtZip.DataBindings.Add _ ("Text", Me.bsCustomers, "Zip", True) navCustomers.BindingSource = Me.bsCustomers End IfEnd Sub 要"保存"的代码Customers表单中的信息是:Private Sub SaveToolStripButton_Click(sender As Object, _ e As EventArgs) _ Handles SaveToolStripButton.Click 'Purpose: Save changes into the Customers Table 'Local Variables: daSQL As SqlDataAdapter ' cbSQL As SqlCommandBuilder ' strSQL As String 'Returns: Nothing - Table is updated inside the subroutine Dim daSQL As SqlDataAdapter Dim cbSQL As SqlCommandBuilder Dim strSQL As String 'End edits to Navigator control navCustomers.BindingSource.EndEdit() Using cnn As New SqlConnection(strSQLCnn) Try If cnn.State = ConnectionState.Open Then cnn.Close() Else cnn.Open() strSQL = "SELECT * FROM dbo.tblCustomers;" daSQL = New SqlDataAdapter(strSQL, cnn) 'Build Commands for add, delete, and update cbSQL = New SqlCommandBuilder(daSQL) 'Update the Customer table daSQL.Update(bsCustomers.DataSource) Msg = "The dbo.tblCustomers table from the SFDC_SQL_MDI " & _ "database has been successfully updated." MessageBox.Show(Msg) End If Catch ex As Exception 'Log error Dim el As New Log.ErrorLogger el.WriteToErrorLog(ex.Message, ex.StackTrace, "Error") End Try End UsingEnd Sub 我想我在SaveToolStripButton_Click事件中没有正确使用。当我逐步完成事件时,我激活了DataSet Visualizer daSQL.Update(bsCustomers.DataSource) 它显示一个空表。I think I am not using something properly in the SaveToolStripButton_Click event. When I step through the event I activated I the DataSet Visualizer at daSQL.Update(bsCustomers.DataSource)It shows an empty table. MRM256推荐答案 看起来你正在制作它远比它应该复杂得多。It looks like you are making it far more sophisticated than it should be.您的问题,也许是您在更新之前没有在绑定源上调用.EndEdit的事实,或者至少我没有看到您的调用代码任何地方Your problem, maybe the fact you are not calling .EndEdit on the bindingsource prior to the update, or at least I dont see the call in your code anyplace 这篇关于使用BindingNavigator和BindingSource保存数据时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-10 06:26