SQL wont save data(Urgent) RRS feed

  • Question

  • I know that Im missing a line or two but im not sure how to put them in ,in order for my sql to work , i keep recieving error 26 

    im trying liink a local database sql to my vb form 

    He is Code :

    Imports System.Data.SqlClient
    Public Class Form5
        Dim connection As String = My.Settings.Database2ConnectionString
        Dim connectionobject, connectionobject2 As New SqlClient.SqlConnection(connection)

        Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
            Dim selectqry As String = "SELECT * FROM tblUsers Where Username= @Username"
            Dim selectcmd As New SqlClient.SqlCommand(selectqry, connectionobject)

            selectcmd.Parameters.Add("Username", SqlDbType.Char).Value = txtUserName.Text

            If connectionobject.State = ConnectionState.Closed Then connectionobject.Open()
            Dim reader As SqlClient.SqlDataReader = selectcmd.ExecuteReader

            If reader.Read = True Then
                MsgBox("Username Alread Exist,Please Enter a different Username")

                Dim InsertQuery As String = "Insert INTO tblUsers(ForeName,SecondName,Target Grade,TestScore1,TestScore2,TestScore3,TestScore4,TestScore5,Teacher,UserName,Password)VALUES(@ForeName,@SecondName,@Target Grade,@TestScore1,@TestScore2,@TestScore3,@TestScore4,@TestScore5,@Teacher,@UserName,@Password)"
                Dim insertcmd As New SqlClient.SqlCommand(InsertQuery, connectionobject2)

                insertcmd.Parameters.Add("ForeName", SqlDbType.NVarChar).Value = txtForename.Text
                insertcmd.Parameters.Add("SecondName", SqlDbType.NVarChar).Value = txtSecondName.Text
                insertcmd.Parameters.Add("Target Grade", SqlDbType.NVarChar).Value = TxtTargetGrade.Text
                insertcmd.Parameters.Add("Teacher", SqlDbType.NVarChar).Value = TxtTeacher.Text
                insertcmd.Parameters.Add("UserName", SqlDbType.NVarChar).Value = txtUserName.Text
                insertcmd.Parameters.Add("Password", SqlDbType.NVarChar).Value = txtPassword.Text
                If connectionobject2.State = ConnectionState.Closed Then connectionobject2.Open()

                MsgBox("Data Successfully Inserted")
            End If


        End Sub
    End Class

    Tuesday, September 22, 2015 8:05 PM


All replies

  • Hi,

    Are you getting Error 26 ?

    Basically this just means that the SQL Server cannot be found. Possible reasons are for example:

    1. wrong connection string
    2. firewall blocking the traffic
    3. correct protocol not enabled
    4. SQL Server is not running and so on

    Try going through this article:


    Thanks, If my reply is helpful please mark as answer or vote as helpful.

    Tuesday, September 22, 2015 8:14 PM
  • Hmm could it be that i dont have microsoft sql installed , im using a local database created in visual studio express ?
    Tuesday, September 22, 2015 8:40 PM
  • Yes it could be.

    Please verify all possibilities and also try to execute your query on database.

    Thanks, If my reply is helpful please mark as answer or vote as helpful.

    Tuesday, September 22, 2015 8:48 PM
  • A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

    Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

    this is the error receive when i enter data into all the test boxes to register , is it something wrong with my code ?

    Tuesday, September 22, 2015 9:02 PM
  • The reasons for throwing this issue could be wrong server name, disabled remote connection and firewall blocking.

    Please follow the steps below to solve this issue;

    1. Check the server on which SQL Server is running can be accessible.
    2. Choose appropriate protocol
    3. Configure Windows firewall accordingly based on what protocol you have chosen to use. 
    4. Enable SQL Server Browser Services

    Please check your connection string in code, please share with us.

    Thanks, If my reply is helpful please mark as answer or vote as helpful.

    Tuesday, September 22, 2015 9:10 PM
  • Downloading microsoft sql and trying them will let you know how it goes 

    Tuesday, September 22, 2015 9:16 PM
  • Yes sure. Above thread is more related to visual studio, please move your thread to visual studio,


    Please keep update if you have any issue.

    Please refer article to save data in SQL database;


    How to: Insert New Records into a Database

    Thanks, If my reply is helpful please mark as answer or vote as helpful.

    Tuesday, September 22, 2015 9:27 PM