1.11M Members

VB2010 - my 1st Data Base

 
0
 

i'm building a data base.. heres what i did:
1 - create the forms and put the controls;
2 - create the Service-base DataBase(add New Item);
3 - a toolbox, on left, is showed and then i create a new table;
4 - using the mouse right button, i select(on menu) Show Table Data for add the new elements.
and now heres the code:
Module:

Imports System.Data.SqlClient

Module Module1
    Public cnADONetConnection As New SqlConnection()
    Public m_cn As New SqlConnection()
    Public m_DA As SqlDataAdapter
    Public m_CB As SqlCommandBuilder
    Public m_DataTable As New DataTable
    Public m_rowPosition As Integer = 0
    Public m_DataRow As DataRow = m_DataTable.Rows(0)


End Module

on form1(where are the menus):

Imports System.Data.SqlClient

Public Class frmMenu

    Private Sub lblAdicionarVeiculo_Click(sender As System.Object, e As System.EventArgs) Handles lblAdicionarVeiculo.Click
        frmInserirDadosVeiculo.ShowDialog()
    End Sub

    Private Sub lblEditarVeiculo_Click(sender As System.Object, e As System.EventArgs) Handles lblEditarVeiculo.Click
        Search.ShowDialog()
    End Sub

    Private Sub lblApagarVeiculo_Click(sender As System.Object, e As System.EventArgs) Handles lblApagarVeiculo.Click
        Search.ShowDialog()
    End Sub

    Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles Label1.Click
        End
    End Sub

    Private Sub frmMenu_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        m_cn.ConnectionString = "Data Source=.\SQLEXPRESS; AttachDbFilename = " & _
"C:\ManutencaoDeAutomoveis\ManutencaoDeAutomoveis\Database1.mdf; " & _
"Integrated Security=True; Connect Timeout=30;" & "User Instance=True"
        m_cn.Open()
        m_DA = New SqlDataAdapter("Select * From Manutencao", m_cn)
        m_CB = New SqlCommandBuilder(m_DA)
        m_DA.Fill(m_DataTable)

    End Sub
End Class

and the form2:

Public Class frmInserirDadosVeiculo

    Private Sub ShowCurrentRecord()
        If m_DataTable.Rows.Count > 0 Then
            txtMarcaModelo.Text = _
        m_DataTable.Rows(m_rowPosition)("IDMarcaModelo").ToString()
            txtMatricula.Text = _
            m_DataTable.Rows(m_rowPosition)("IDMatricula").ToString()
        End If

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub frmInserirDadosVeiculo_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.ShowCurrentRecord()
    End Sub
End Class

when i run the application i get 2 errors on form1:

m_cn.ConnectionString = "Data Source=.\SQLEXPRESS; AttachDbFilename = " & _
"C:\ManutencaoDeAutomoveis\ManutencaoDeAutomoveis\Database1.mdf; " & _
"Integrated Security=True; Connect Timeout=30;" & "User Instance=True"

error messages:

A first chance exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll
A first chance exception of type 'System.TypeInitializationException' occurred in ManutencaoDeAutomoveis.exe

what i'm doing wrong?
i'm trying learn it from: http://www.vbtutor.net/index.php/visual-basic-2010-lesson-30/
but i don't understand what isn't right :(
please anone tell me something :(

 
0
 

I think the Dim statements in the module don't work like this. You need to dim the databse stuff in the event where you are using them. Also put your error msg into google and you will most likely find some explanation.

Question Answered as of 6 Months Ago by Minimalist
 
0
 

the IDE was tell me these line was an error:

 m_cn.ConnectionString = "Data Source=.\SQLEXPRESS; AttachDbFilename = " & _
"C:\ManutencaoDeAutomoveis\ManutencaoDeAutomoveis\Database1.mdf; " & _
"Integrated Security=True; Connect Timeout=30;" & "User Instance=True"

but isn't correct. that line it's ok.. the problem was here:

Public m_DataRow As DataRow = m_DataTable.Rows(0)

how can i inicializate that variable without know what table is :)
after fix that line, the problem was resolved too ;)
thanks for all

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article