Overload resolution failed

Marvin_G

Member
Joined
Jul 30, 2013
Messages
7
Programming Experience
1-3
Hello below is my code, i got this error Error "Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:'Public SubNew(selectCommandText As String, selectConnection As System.Data.OleDb.OleDbConnection)': Argument matching parameter 'selectConnection' narrows from 'Object' to 'System.Data.OleDb.OleDbConnection'. 'Public Sub New(selectCommandText As String, selectConnectionString As String)': Argument matching parameter 'selectConnectionString' narrows from 'Object' to 'String')

What did i do is i declared "Public Shared cnn = New OleDb.OleDbConnection" in a class to make it global.

Then i called it in other class.

VB.NET:
 Try
            'get data into list
            If Not GlblVar.cnn.State = ConnectionState.Open Then
                'open connection
                GlblVar.cnn.Open()
            End If


            Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM student ", GlblVar.cnn)


            'fill data to datatable
            da.Fill(GlblVar.dt)


            'close connection
            GlblVar.cnn.Close()
        Catch ex As Exception
            MessageBox.Show("ERROR" & ex.Message)
        End Try


how can i solve this?
 
The issue is that, because you haven't specified the type for your 'cnn' variable, it is assumed to be type Object. When you then create your data adapter, it can't decide which constructor you intend to invoke. You should start by turning Option Strict On in the project properties, which will then not allow you to declare variables without a specific type. You should also turn it On in the IDE options so that it's On by default for all future projects. You will then have to change this:
VB.NET:
Public Shared cnn [B][COLOR="#FF0000"]=[/COLOR][/B] New OleDb.OleDbConnection
to this:
VB.NET:
Public Shared cnn [B][COLOR="#FF0000"]As[/COLOR][/B] New OleDb.OleDbConnection
By the way, your code to open and close the connection is pointless. Calling Fill or Update will automatically open and close the connection by default. If you actually were going to open and close the connection then the close should be done in a finally block, because Close will not be called in that code if an exception is thrown. That's the whole point of a Finally block.
 
1 more question, i did this in my code

Public Sub dbInsert(ByVal name, ByVal gender, ByVal phone, ByVal address)
Try
If Not GlblVar.cnn.State = ConnectionState.Open Then
'open connection if it is not yet open
GlblVar.cnn.Open()
End If


GlblVar.cmd.Connection = GlblVar.cnn
GlblVar.cmd.CommandText = "INSERT INTO student stdname, gender, phone, address) VALUES('" & name & "','" & gender & "','" & phone & "','" & address & "')"
GlblVar.cmd.ExecuteNonQuery()

why did i get error " Operator "&" is not defined for string "INSERT INTO student(stdname,gen" and type 'TextBox' "?
 
Last edited:
For future reference, please ask new questions in new threads unless they are directly related to the topic of the current thread, which this is not.

Anyway, the issue is that you're trying to insert the actual TextBox control into a String, which doesn't make sense. That's like actually putting a person into a list rather than their name. It's the Text of the TextBox you want, not the TextBox itself.

Also, it's bad practice to use string concatenation to insert values into SQL code. The most obvious issue is if someone's name or address has an apostrophe in it. The proper method is to use parameters. To learn why and how, follow the Blog link in my signature and check out my post on Parameters In ADO.NET.
 
Ah, beaten to it by jmcilhinney. One additional point however, see my point 1 below:-

Hi,

Firstly, if you have a separate question, unrelated to the original question, then you should always create a new thread. In this case I have a couple of points for you here:-

This statement:-

VB.NET:
GlblVar.cmd.CommandText = "INSERT INTO student stdname, gender, phone, address) VALUES('" & Name & "','" & gender & "','" & phone & "','" & address & "')"

Has two errors.

1) You are missing the Opening Bracket, after the Table Name Student, which encases your destination field names.
2) You are trying to assign the ACTUAL TextBox Controls to your SQL Query when you should be concatenating the Text Values of each of the TextBox controls. i.e:-

VB.NET:
Name.Text, Gender.Text etc....

That said, you should be using Parameters when assigning the values of objects to an SQL String in your project. Have a look here as to how you can do this and the reasons for doing this:-

John McIlhinney's .NET Developer Blog: Using Parameters in ADO.NET

Hope that helps.

Cheers,

Ian
 
Last edited:
Back
Top