none
conexao com banco access RRS feed

  • Pergunta

  • Consigo conectar com o banco access e consigo incluir,

    mas para criar a combo de itens cadastrados , recuperar dados, incluir , alterar e excluir

    dá o seguinte erro :

    The ConnectionString property has not been initialized. >>>

     

    objConexao.Open()

     

    Public Module Modulo

    Public strConexao As String

    End Module

     

    Imports System.Data

    Imports System.Data.OleDb

    Partial Class _Default

    Inherits System.Web.UI.Page

    Dim objConexao As OleDbConnection

    Public Modulo As Global._Default

    Dim strConexao As String

    Dim blnSetafoco As Boolean

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender

    ' If IsPostBack Then

    'If Not blnSetafoco Then

    'txtNome.Attributes.Add("onFocus", "__doPostBack('txtNomeCliente','onFocus')")

    ' Else

    ' txtNome.Attributes.Add("onFocus", "")

    ' End If

    ' End If

     

     

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Modulo = New Global._Default

    objConexao = New OleDbConnection

    Session("conexao") = objConexao

    GeracomboEmpregados()

    End Sub

    Private Sub Limpatela()

    comboEmpregados.Visible = True

    txtNome.Text = " "

    txtprofissao.Text = " "

    comboEmpregados.SelectedValue = -1

    End Sub

    Protected Sub cmdincluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdincluir.Click

    Dim objComando As OleDbCommand

    Dim strSql As String

    Dim intlinhas As Integer

    Dim objTransacao As OleDbTransaction

    Dim blnTransacao As Boolean = False

     

    strConexao = "Provider=Microsoft.JET.OLEDB.4.0;" & _

    "Data Source =C:\Documents and Settings\Administrador\Desktop\cristiano\bd2.mdb"

    '"Data Source =C:\Documents and Settings\Administrador\Meus documentos\Visual Studio 2008\Projects\WebSite3\bd2.mdb"

     

    With objConexao

    .ConnectionString = strConexao

    .Open()

    End With

    strSql = "INSERT INTO empregado" & vbCrLf & _

    "(nome,profissao)" & vbCrLf & _

    " VALUES (' " & txtNome.Text & " ',' " & txtprofissao.Text & " ') "

    objComando = New OleDbCommand

    objComando.Connection = objConexao

    objComando = objConexao.CreateCommand

    If objComando.Connection.State <> ConnectionState.Open Then

    objConexao.ConnectionString = strConexao

    objConexao.Open()

    End If

    objTransacao = objConexao.BeginTransaction

    blnTransacao = True

    With objComando

    .Transaction = objTransacao

    .CommandType = CommandType.Text

    .CommandText = strSql

    intlinhas = .ExecuteNonQuery

    End With

    objTransacao.Commit()

    blnTransacao = False

    Call Limpatela()

     

    Call GeracomboEmpregados()

    objComando = Nothing

     

    End Sub

    Private Sub GeracomboEmpregados()

    'Dim intI As Integer

    ' Dim intTotal As Integer

    ' Dim intlinhas As Integer

    'objConexao = New OleDbConnection

    Dim objcomando As OleDbCommand

    ' Dim objConexao As OleDbConnection

    Dim objLeitura As OleDbDataReader

    Dim StrSql As String

     

     

    ' strConexao = "Provider=Microsoft.JET.OLEDB.4.0;" & _

    ' "Data Source =C:\Documents and Settings\Administrador\Desktop\cristiano\bd2.mdb"

    ' With objConexao

    'ConnectionString = strConexao

    ' .Open()

    ' End With

    StrSql = "SELECT codigo, " & vbCrLf & _

    " nome " & vbCrLf & _

    " FROM empregado"

     

     

    If objConexao.State <> ConnectionState.Open Then

    objConexao.Open() 

    End If

    objcomando = objConexao.CreateCommand

    With objcomando

    .CommandText = StrSql

    .CommandType = CommandType.Text

    objLeitura = .ExecuteReader

    End With

    With objLeitura

    comboEmpregados.Items.Clear()

    comboEmpregados.Items.Add("CADASTRE EMPREGADO")

    comboEmpregados.Items.FindByText("CADASTRE EMPREGADO").Value = -1

    While .Read

    comboEmpregados.Items.Add(.Item("nome"))

    comboEmpregados.Items.FindByText(.Item("nome")).Value = .Item("codigo")

    End While

    comboEmpregados.Items.Add(" ")

    comboEmpregados.Items.FindByText(" ").Value = -2

    comboEmpregados.SelectedValue = -1

    End With

    objConexao.Close()

     

     

     

    End Sub

    Protected Sub comboEmpregados_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboEmpregados.Load

    If Not IsPostBack Then

    GeracomboEmpregados()

    End If

    End Sub

    Protected Sub comboEmpregados_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboEmpregados.SelectedIndexChanged

    If comboEmpregados.SelectedValue = -1 Then

    txtNome.Visible = True

    comboEmpregados.Visible = False

    Else

    txtNome.Visible = False

    comboEmpregados.Visible = True

    RecuperaCodigo()

    End If

    End Sub

    Private Sub RecuperaCodigo()

    If comboEmpregados.SelectedValue = -2 Then

    txtcodigo.Text = " "

    txtNome.Text = " "

    txtprofissao.Text = " "

    Else

    txtcodigo.Text = comboEmpregados.SelectedValue

    'Dim intI As Integer

    'Dim intTotal As Integer

    Dim objComando As OleDbCommand

    Dim objLeitura As OleDbDataReader

    Dim StrSql As String

    ' Dim intlinhas As Integer

    ' strConexao = "Provider=Microsoft.JET.OLEDB.4.0;" & _

    ' "Data Source =C:\Documents and Settings\Administrador\Desktop\cristiano\bd2.mdb"

    ' With objConexao

    '.ConnectionString = strConexao

    ' .Open()

    ' End With

     

     

     

    StrSql = " SELECT * " & vbCrLf & _

    " FROM empregado " & vbCrLf & _

    " WHERE codigo = " & txtcodigo.Text

     

    If objConexao.State <> ConnectionState.Open Then

    objConexao.Open()

    End If

    objComando = objConexao.CreateCommand

     

    With objComando

    .CommandText = StrSql

    .CommandType = CommandType.Text

    objLeitura = .ExecuteReader

    End With

    With objLeitura

    If .Read Then

    txtNome.Text = .Item("nome")

    txtprofissao.Text = .Item("profissao")

    End If

    End With

    objConexao.Close()

    End If

    End Sub

    Protected Sub cmdAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdAlterar.Click

    Dim objComando As OleDbCommand

    ' Dim strConexao As String

    Dim strSql As String

    Dim intlinhas As Integer

    Dim objTransacao As OleDbTransaction

    Dim blnTransacao As Boolean = False

    ' strConexao = "Provider=Microsoft.JET.OLEDB.4.0;" & _

    ' "Data Source =C:\Documents and Settings\Administrador\Desktop\cristiano\bd2.mdb"

    ' With objConexao

    ' .ConnectionString = strConexao

    ' .Open()

    ' End With

     

    strSql = " UPDATE empregado " & vbCrLf & _

    " SET nome =' " & txtNome.Text & " ', " & vbCrLf & _

    " profissao = ' " & txtprofissao.Text & " ' " & vbCrLf & _

    " WHERE codigo = " & txtcodigo.Text

     

    objComando = New OleDbCommand

    objComando.Connection = objConexao

    objComando = objConexao.CreateCommand

    If objConexao.State <> ConnectionState.Open Then

    objConexao.ConnectionString = strConexao

    objConexao.Open()

    End If

    objTransacao = objConexao.BeginTransaction

    blnTransacao = True

     

    With objComando

    .Transaction = objTransacao

    .CommandType = CommandType.Text

    .CommandText = strSql

    intlinhas = .ExecuteNonQuery()

    End With

    objTransacao.Commit()

    blnTransacao = False

    objComando = Nothing

     

     

    End Sub

     

    Protected Sub cmdExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdExcluir.Click

    Dim objComando As OleDbCommand

    Dim objTransacao As OleDbTransaction

    Dim objLeitura As OleDbDataReader

    Dim blnTransacao As Boolean = False

    Dim StrSql As String

    Dim intLinhas As Integer

    ' Dim strConexao As String

     

    ' strConexao = "Provider=Microsoft.JET.OLEDB.4.0;" & _

    '"Data Source =C:\Documents and Settings\Administrador\Desktop\cristiano\bd2.mdb"

     

    ' With objConexao

    '.ConnectionString = strConexao

    '.Open()

    ' End With

     

    StrSql = " DELETE * from empregado" & vbCrLf & _

    " WHERE codigo = " & txtcodigo.Text

    objComando = New OleDbCommand

    objComando.Connection = objConexao

    objComando = objConexao.CreateCommand

    If objConexao.State <> ConnectionState.Open Then

    objConexao.ConnectionString = strConexao

    objConexao.Open()

    End If

    objTransacao = objConexao.BeginTransaction

    blnTransacao = True

    With objComando

    .Transaction = objTransacao

    .CommandText = StrSql

    .CommandType = CommandType.Text

    intLinhas = .ExecuteNonQuery

    End With

    objTransacao.Commit()

    blnTransacao = False

    With comboEmpregados

    .Items.RemoveAt(.SelectedIndex)

    End With

    Call Limpatela()

    End Sub

    End Class

    terça-feira, 20 de maio de 2008 22:57

Todas as Respostas

  • Você definiu a string de conexão do seu objeto de conexão em escopo local no evento click, portanto não é reconhecida em outro evento.

    Tem que definir novamente.

    quarta-feira, 21 de maio de 2008 17:15