主题:大哥,大姐们帮我看看这段代码吧,老是不能运行!帮帮我
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub T1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T1BindingNavigatorSaveItem.Click
Dim cn1 As String
Dim dad As New SqlDataAdapter
Dim ds As New DataSet
Dim t As New DataTable
Dim r As DataRow
Dim id As Integer
Dim str As String = 1
Dim cmd As New SqlCommandBuilder(dad)
cn1 = "data source=.\sqlexpress;attachdbfilename=" + Application.StartupPath
cn1 = cn1 + "\kk.mdf;integrated security=true;connection timeout=30;user instance=true"
Dim cn As SqlConnection = New SqlConnection(cn1)
id = Integer.Parse(str)
If id = t.Rows.Count + 1 Then
r = t.NewRow
r(1) = Ft.Text
r(2) = Lt.Text
t.Rows.Add(r)
dad.Update(ds, "T1")
MessageBox.Show("added successfully")
Me.BindingNavigatorAddNewItem.Text = "add"
Me.BindingNavigatorAddNewItem.Enabled = True
Me.BindingNavigatorDeleteItem.Enabled = True
Me.T1BindingNavigatorSaveItem.Enabled = False
ElseIf id < t.Rows.Count + 1 Then
r = t.Rows(id - 1)
r("f_name") = Ft.Text
r("l_name") = Lt.Text
dad.Update(ds, "T1")
Me.T1BindingNavigatorSaveItem.Enabled = False
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: 这行代码将数据加载到表“Database1DataSet.T1”中。您可以根据需要移动或移除它。
Me.T1TableAdapter.Fill(Me.KkDataSet.T1)
End Sub
Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
Dim rc As Integer
Dim dad As New SqlDataAdapter
Dim ds As New DataSet
Dim t As New DataTable
If MessageBox.Show("Do you really want to Delete this Record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
Dim cmd As New SqlCommandBuilder(dad)
t.Rows(rc).Delete()
MsgBox("deleted successfully")
dad.Update(ds, "T1")
End Sub
Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
Dim t As New DataTable
Try
If BindingNavigatorAddNewItem.Text = "add" Then
Ft.Focus()
IdT.Text = Integer.Parse(t.Rows(t.Rows.Count - 1).Item(0)) + 1
IdT.ReadOnly = True
Ft.ReadOnly = False
Lt.ReadOnly = False
Ft.Clear()
Lt.Clear()
Me.BindingNavigatorDeleteItem.Enabled = False
Me.T1BindingNavigatorSaveItem.Enabled = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub getemp(ByVal i As Integer)
Dim t As New DataTable
Dim r As DataRow
r = t.Rows(i)
IdT.Text = r("id")
Ft.Text = r("f_name")
Lt.Text = r("l_name")
End Sub
Public Sub formload()
Me.IdT.ReadOnly = True
Me.Ft.ReadOnly = True
Me.Lt.ReadOnly = True
IdT.Clear()
Ft.Clear()
Lt.Clear()
Me.BindingNavigatorAddNewItem.Enabled = True
Me.T1BindingNavigatorSaveItem.Enabled = False
Me.BindingNavigatorDeleteItem.Enabled = False
End Sub
End Class
Imports System.Data.SqlClient
Public Class Form1
Private Sub T1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles T1BindingNavigatorSaveItem.Click
Dim cn1 As String
Dim dad As New SqlDataAdapter
Dim ds As New DataSet
Dim t As New DataTable
Dim r As DataRow
Dim id As Integer
Dim str As String = 1
Dim cmd As New SqlCommandBuilder(dad)
cn1 = "data source=.\sqlexpress;attachdbfilename=" + Application.StartupPath
cn1 = cn1 + "\kk.mdf;integrated security=true;connection timeout=30;user instance=true"
Dim cn As SqlConnection = New SqlConnection(cn1)
id = Integer.Parse(str)
If id = t.Rows.Count + 1 Then
r = t.NewRow
r(1) = Ft.Text
r(2) = Lt.Text
t.Rows.Add(r)
dad.Update(ds, "T1")
MessageBox.Show("added successfully")
Me.BindingNavigatorAddNewItem.Text = "add"
Me.BindingNavigatorAddNewItem.Enabled = True
Me.BindingNavigatorDeleteItem.Enabled = True
Me.T1BindingNavigatorSaveItem.Enabled = False
ElseIf id < t.Rows.Count + 1 Then
r = t.Rows(id - 1)
r("f_name") = Ft.Text
r("l_name") = Lt.Text
dad.Update(ds, "T1")
Me.T1BindingNavigatorSaveItem.Enabled = False
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: 这行代码将数据加载到表“Database1DataSet.T1”中。您可以根据需要移动或移除它。
Me.T1TableAdapter.Fill(Me.KkDataSet.T1)
End Sub
Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
Dim rc As Integer
Dim dad As New SqlDataAdapter
Dim ds As New DataSet
Dim t As New DataTable
If MessageBox.Show("Do you really want to Delete this Record?", _
"Delete", MessageBoxButtons.YesNo, _
MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
Dim cmd As New SqlCommandBuilder(dad)
t.Rows(rc).Delete()
MsgBox("deleted successfully")
dad.Update(ds, "T1")
End Sub
Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
Dim t As New DataTable
Try
If BindingNavigatorAddNewItem.Text = "add" Then
Ft.Focus()
IdT.Text = Integer.Parse(t.Rows(t.Rows.Count - 1).Item(0)) + 1
IdT.ReadOnly = True
Ft.ReadOnly = False
Lt.ReadOnly = False
Ft.Clear()
Lt.Clear()
Me.BindingNavigatorDeleteItem.Enabled = False
Me.T1BindingNavigatorSaveItem.Enabled = True
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub getemp(ByVal i As Integer)
Dim t As New DataTable
Dim r As DataRow
r = t.Rows(i)
IdT.Text = r("id")
Ft.Text = r("f_name")
Lt.Text = r("l_name")
End Sub
Public Sub formload()
Me.IdT.ReadOnly = True
Me.Ft.ReadOnly = True
Me.Lt.ReadOnly = True
IdT.Clear()
Ft.Clear()
Lt.Clear()
Me.BindingNavigatorAddNewItem.Enabled = True
Me.T1BindingNavigatorSaveItem.Enabled = False
Me.BindingNavigatorDeleteItem.Enabled = False
End Sub
End Class