Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB
Try
            conn.ConnectionString = My.Settings.Database1ConnectionString
            conn.Open()
 
            cmd.Connection = conn
            cmd.CommandText = "SELECT passid FROM tblogin Where user=@user"
            cmd.Parameters.AddWithValue("@user", txtcod_ut.Text)
            passid = cmd.ExecuteScalar
 
            conn.Close()
  If suportelogin.GeraHash(txtpass.Text).Equals(passid) Then
                My.Forms.Página_Principal.ShowDialog()
                Me.Hide()
            Else
                MessageBox.Show("Erro!.", "Verifique de novo o código do utilizador e a palavra-passe", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                'focus- quando for precionado o botão "enter" do teclado, ele vai ativar esse controle como Ok
                txtpass.Focus()
            End If
        Catch ex As Exception
 
            MessageBox.Show("Erro ao efetuar a conexão com a base de dados :" + ex.Message)
            conn.Dispose()
 
        End Try
Posted 19-Apr-13 2:45am
Edited 19-Apr-13 2:57am
_duDE_8.1K
v2
Comments
Orcun Iyigun at 19-Apr-13 7:47am
   
Debug your code and tell us which line it is throwing the exception?
Nillo123 at 19-Apr-13 7:57am
   
conn.ConnectionString = My.Settings.Database1ConnectionString
 
conn returns nothing
i checked if is null..and nothing happen
Aarti Meswania at 19-Apr-13 7:48am
   
in which line you get this exception?
ryanb31 at 19-Apr-13 7:48am
   
What line is it? The error is telling you that something you are trying to access has not been initialized or does not exist.
Nillo123 at 19-Apr-13 7:55am
   
conn.ConnectionString = My.Settings.Database1ConnectionString
 
conn returns nothing
Richard MacCutchan at 19-Apr-13 7:59am
   
Where is conn declared? Also check that the sttings variable is correct.
Nillo123 at 19-Apr-13 8:04am
   
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class Login
 
Private Sub butcancel_Click(sender As Object, e As EventArgs) Handles butcancel.Click
Me.Close()
End Sub
Dim conn As SqlConnection
Dim cmd As SqlCommand
Dim passid As String

Private Sub butok_Click(sender As Object, e As EventArgs) Handles butok.Click

conn.ConnectionString = My.Settings.Database1ConnectionString
ThePhantomUpvoter at 19-Apr-13 8:08am
   
You are just declaring a variable of SqlConnection. You need to create an instantiation of the SqlConnection class before you can use it. The error is telling you it is null because it is null when you try and use it.
Nillo123 at 19-Apr-13 8:17am
   
can you give me an example , please.
ThePhantomUpvoter at 19-Apr-13 8:40am
   
You are joking right?
Dim conn As SqlConnection = new SqlConnection(yourconnectionstring)
Nillo123 at 19-Apr-13 9:01am
   
i am new at this..
Nillo123 at 19-Apr-13 9:01am
   
thanks for the help)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Why do I get the error "Object reference not set to an instance of an object"?[^].
 
Quote:
conn.ConnectionString = My.Settings.Database1ConnectionString
 
conn returns nothing

 
It is NOT 'conn returns nothing', it is 'conn is nothing' instead. That means your conn variable does not contain a reference to a valid (Connection) object.
Probably you are using conn before its proper initialization code is called.
  Permalink  
Comments
Nillo123 at 19-Apr-13 8:16am
   
if i put the : 'conn.ConnectionString = My.Settings.Database1ConnectionString' in try my problems beggins on connDispose()
Try
conn.ConnectionString = My.Settings.Database1ConnectionString
conn.Open()

cmd.Connection = conn
.
.
.
conn.Dispose()
 
msg error : Object reference not set to an instance of an object...
 
if i remove 'conn.ConnectionString = My.Settings.Database1ConnectionString' from the try.. he gives me the error on beggining...like you said before..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 162
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 19 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100