Hello guys i have a question i have these codes

Imports System.Data
Imports System.Data.SqlClient

Module Connect
    Public Conn As SqlConnection

    Public Function GetConnect()

        Conn = New SqlConnection("Server = SQL Server;" & "initial Catalog = BloodBank;" & " Trusted_Connection=yes")
        Return Conn

    End Function
End Module
Public Class BloodDonor

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtState.TextChanged

    End Sub
    Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click

    End Sub

    Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        Dim strBloodType As String = txtBloodType.Text
        Dim strDonorName As String = txtDonorName.Text
        Dim strEMail As String = txtEMail.Text
        Dim strPhoneNumber As String = txtPhoneNumber.Text
        Dim strLocation As String = txtLocation.Text
        Dim strState As String = txtState.Text
        Dim strStatus As String = txtStatus.Text
        Dim strIDNumber As String = txtIDNumber.Text

        Dim strCommand As String = "insert into BloodBank values('" & strBloodType & "', '" & strDonorName & "', '" & strEMail & "', '" & strPhoneNumber & "', '" & strLocation & "', '" & strState & "', '" & strStatus & "', '" & strIDNumber & ")"

        Dim Command As SqlClient.SqlCommand = New SqlClient.SqlCommand(strCommand, GetConnect)
        Command.CommandType = CommandType.Text

        '' MsgBox(strCommand)

        Conn.Open()
        If (Command.ExecuteNonQuery().Equals(1)) Then
            MsgBox("Information Stored In Database")
        Else
            MsgBox("Information Didn't Get Store In Database")
        End If
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click



    End Sub

    Private Sub BloodDonor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

When i run it it gives me an error on Conn.Open() i would really like your help this is the first time i've ever connected VB.Net to an SQL database.

Re: VB.Net SQL Connection Question 80 80

Example:

page1.vb

Partial Class sample
  Inherits System.Web.UI.Page

Dim conn As String = ConfigurationManager.ConnectionStrings("dbsample").ConnectionString
Dim sqlconn As New SqlConnection(conn)

Public sub test()
    sqlconn.open()
        .....sample code
    sqlconn.close()
End Sub

web.config

<connectionStrings>
    <add name="dbsample" connectionString="Data Source=Databasename;Password=Passw0rd;
     User ID=SampleID;Initial Catalog=Sample_Catalog"/>
</connectionStrings>
commented: How do i use it, i would be glad if you explain what you did, why do i need the Web.UI.Page for ? +0
Re: VB.Net SQL Connection Question 80 80

It would be nice if you could tell me what did you do and why, because i didn't get it ..

Re: VB.Net SQL Connection Question 80 80

Hi,

You are getting the error, because, you have not populated the connection string for the connection... call the function getConnect.. and open the connection...

Connect.GetConnect()
Conn.Open()

Regards
Veena

commented: Getting same error :( +0
Re: VB.Net SQL Connection Question 80 80

Hi,

You are getting the error, because, you have not populated the connection string for the connection... call the function getConnect.. and open the connection...

Connect.GetConnect()
Conn.Open()
Regards
Veena

Hi,

You are getting the error, because, you have not populated the connection string for the connection... call the function getConnect.. and open the connection...

Connect.GetConnect()
Conn.Open()
Regards
Veena

Same error (https://www.dropbox.com/s/fpvkpfipwqeu9wj/Problem%201.png)

Re: VB.Net SQL Connection Question 80 80

You have to specify a particular SQL Server instance in the connection string. For example, on my computer it is

"Server=.\SQLEXPRESS;Database=PUBS;Trusted_Connection=yes;"

or

"Server=JIM-PC\SQLEXPRESS;Database=PUBS;Trusted_Connection=yes;"
Re: VB.Net SQL Connection Question 80 80

Hi,

In your connectionstring ... you have to mention Servername..

"Server = SQL Server;"

Regards
Veena

Re: VB.Net SQL Connection Question 80 80

Thank you both for your help, now it gives me this error (https://www.dropbox.com/s/8uc2afzxrbwxcdk/Problem%202.png) saying that the login failed.
I tested the connection in "Modify Connection" and the connection succedded.

This is my Connect Module

Module Connect
    Public Conn As SqlConnection

    Public Function GetConnect()

        Conn = New SqlConnection("Server = .\SQLEXPRESS;Database=SmartAidDB;Trusted_Connection=yes;")
        Return Conn

    End Function
End Module
Re: VB.Net SQL Connection Question 80 80

Hi,

To use trusted Connection, you should have logged in Windows using Administrator login..
For other users, it may not work, until privileges are given...
Create user in SQL Server, and give access to the user.. use that userID and pwd in your connstring..

Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;

Regards
Veena

Re: VB.Net SQL Connection Question 80 80

Hi,

To use trusted Connection, you should have logged in Windows using Administrator login..
For other users, it may not work, until privileges are given...
Create user in SQL Server, and give access to the user.. use that userID and pwd in your connstring..

Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;

Regards
Veena

I couldn't do it, unfortunately, i would be glad if you could tell me what to do in steps!
Since i am new to all of this.

Re: VB.Net SQL Connection Question 80 80

It gives me the same error, my Connect Module :

Module Connect
    Public Conn As SqlConnection

    Public Function GetConnect()

        Conn = New SqlConnection("Server = .\SQLEXPRESS;Database=SmartAidDB;Uid=user;Pwd=user;Trusted_Connection=yes")
        Return Conn

    End Function
End Module

What should i do this looks hopeless ?

Re: VB.Net SQL Connection Question 80 80

Hi,

if you are using SQL Express...
You need to mention location of the mdf file...

try this :

Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;
Trusted_Connection=Yes;

Regards
Veena

Re: VB.Net SQL Connection Question 80 80

I think the connection worked but now another error on another line.

Here's the error and the line it occurs in (https://www.dropbox.com/s/0zslhfcti6rad4n/Problem%203.png)

Re: VB.Net SQL Connection Question 80 80

Hi,

Change your code like this:

Conn.Open()
Dim Command As SqlClient.SqlCommand = New SqlClient.SqlCommand(strCommand, Conn)
If (Command.ExecuteNonQuery().Equals(1)) Then

Regards
Veena

Re: VB.Net SQL Connection Question 80 80

Hi,

Change your code like this:

Conn.Open()
Dim Command As SqlClient.SqlCommand = New SqlClient.SqlCommand(strCommand, Conn)
If (Command.ExecuteNonQuery().Equals(1)) Then
Regards
Veena

Now it gives me this error (https://www.dropbox.com/s/i1xcumkchyw6qf6/Problem%204.png)

I would really appreciate it if you check the whole code for the project on Pastebin,
here's a link (http://pastebin.com/8pnmNY9p)

Re: VB.Net SQL Connection Question 80 80

If you are using a trusted connection (ie validation based on your Windows login ID) then you don't specify a userid/password. Also, do not specify the physical database file. Let SQL server handle that. Use

"Server=.\SQLEXPRESS;Database=SmartAidDB;Trusted_Connection=yes;"
Re: VB.Net SQL Connection Question 80 80

If you are using a trusted connection (ie validation based on your Windows login ID) then you don't specify a userid/password. Also, do not specify the physical database file. Let SQL server handle that. Use

"Server=.\SQLEXPRESS;Database=SmartAidDB;Trusted_Connection=yes;"

Same this error pops up (https://www.dropbox.com/s/i1xcumkchyw6qf6/Problem%204.png)

Re: VB.Net SQL Connection Question 80 80

Your original post was for a problem connecting. This is a different problem. Please post your code here. The screenshot you linked to does not show the query text and that's where the problem lies. We will also need to know the contents of strCommand because there could be characters in the values from the textbox controls that are screwing things up. When we get that cleared up I can show you how you can use parameterized queries (not complicated and also safer).

Re: VB.Net SQL Connection Question 80 80

s for a problem connecti

s for a problem connecti

Your original post was for a problem connecting. This is a different problem. Please post your code here. The screenshot you linked to does not show the query text and that's where the problem lies. We will also need to know the contents of strCommand because there could be characters in the values from the textbox controls that are screwing things up. When we get that cleared up I can show you how you can use parameterized queries (not complicated and also safer).

I would be glad if you could exactley tell me what to do, i already have the full code posted before two posted in Pastebin.

Re: VB.Net SQL Connection Question 80 80

Hi,

You have not closed the last single quote...

Dim strCommand As String = "insert into BloodBank values('" & strBloodType & "', '" & strDonorName & "', '" _
    & strEMail & "', '" & strPhoneNumber & "', '" & strLocation & "', '" & strState & "', '" _
    & strStatus & "', '" & strIDNumber & "')"

'If strIDNumber is Numeric, then remove quotes around it

Dim strCommand As String = "insert into BloodBank values('" & strBloodType & "', '" & strDonorName _
    & "', '" & strEMail & "', '" & strPhoneNumber & "', '" & strLocation & "', '" & strState _
    & "', '" & strStatus & "', " & strIDNumber & ")"

Regards
Veena

Re: VB.Net SQL Connection Question 80 80

Hi,

We got over the other problems now we have another problem, i tried to save information into the database, and it said that it got successfully saved (https://www.dropbox.com/s/lwezlmggg14ewsd/Problem%205_1.png).

But i showed the table data nothing is there (https://www.dropbox.com/s/547lz6le08clhz3/Problem%205_2.png?m)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.