none
Onde declaro a strconexão de forma unica para todos os botões e metodos a utilizem ? RRS feed

  • Pergunta

  • Onde declaro a strConexão de maneira única, para que:  a inclusão, alteração exclusão e demais botões e metodos possam funcionar ?

     

    dá sempre o mesmo erro quando vai abrir a conexão em cada botão ou método

    objConexao.Open()

     The ConnectionString property has not been initialized.

     

    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

    sexta-feira, 23 de maio de 2008 18:35

Todas as Respostas

  • Já tentou setar ela dentro do Page_Load?
    []s
    segunda-feira, 26 de maio de 2008 17:05
  • Experimenta usar o web.config e acessar a string de conexão de lá. É melhor porque fica visível na aplicação inteira e você pode alterar direto no arquivo web.config sem ter que mexer no .vb e compilar de novo.

     

    Adicione a chave no web.config da seguinte forma:

     

    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

    <connectionStrings>

    <add name="ConexaoBanco" connectionString="Provider=Microsoft.JET.OLEDB.4.0; Data Source =C:\Documents and Settings\Administrador\Desktop\cristiano\bd2.mdb" providerName="System.Data.OleDb"/>

    </connectionStrings>

    </configuration>

     

     

    Acesse o valor do web.config da seguinte forma de qualquer lugar da aplicação:

     

    With objConexao

    .ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("ConexaoBanco").ConnectionString

    .Open()

    End With

     

     

    Espero ter ajudado

     

    Abraço

    segunda-feira, 26 de maio de 2008 17:58