0

I'm trying to run this program but it gave me an error, I have checked everything like server name, database name, but I don't know where I made a mistake.

Please help out to solve this problem

Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
    Dim con As New SqlConnection("Server=PAULIN\LARIE4; database=TermPaper;")
    Dim da As New SqlDataAdapter()
    Dim dt As New DataTable
    Dim ds As New DataSet

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ld()


End Sub
Private Sub clearControls()
    txtN.Text = ""
    txtP.Text = ""

End Sub
Private Sub clrbinding()
    ds.Tables(0).Rows.Clear()
    dt.Rows.Clear()
    DataGridView1.DataSource = Nothing

End Sub
Private Sub ld()
    da.SelectCommand = New SqlCommand
    da.SelectCommand.Connection = New SqlConnection
    da.SelectCommand.CommandText = "select *from termpaper1"
    da.SelectCommand.CommandType = CommandType.Text
    con.Open()
    da.Fill(ds, "termpaper1")
    con.Close()
    dt = ds.Tables("termpaper1")
    DataGridView1.DataSource = dt
End Sub
Private Sub btnInser_Click(sender As Object, e As EventArgs) Handles btnInser.Click
    clearControls()
    clrbinding()


End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    da.InsertCommand = New SqlCommand
    da.InsertCommand.Connection = New SqlConnection
    da.InsertCommand.CommandText = "insert into termpaper1(user_name,password)values('" & txtN.Text & "','" & txtP.Text & "')"
    da.InsertCommand.CommandType = CommandType.Text
    con.Open()
    da.InsertCommand.ExecuteNonQuery()
    MsgBox("one record added")
    clrbinding()
    ld()

End Sub
End Class
malexander
  • 4,522
  • 1
  • 31
  • 37
larie
  • 5
  • 1
  • 2
  • 4
    "but it gave me an error" - the suspense is killing me! – Mitch Wheat Dec 05 '13 at 01:54
  • 1
    Did you try putting a space b/t the * and the `termpaper1` - `select * from termpaper1` – OneFineDay Dec 05 '13 at 01:54
  • yes i did but stil nothing happened – larie Dec 05 '13 at 01:57
  • Please report any error messages. Please debug your work - are the variable filling with data? So you have the table spelled correctly? – OneFineDay Dec 05 '13 at 02:14
  • @DonA ,the variable are not filling with data, the error occurs when i'm trying to debug my project . it showing a yellow arrow pointing to "con.open()" with this message **An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll** – larie Dec 05 '13 at 03:02
  • @larie - and what is the message in the `SqlException`? Is your connection string correct? – Karl Anderson Dec 05 '13 at 03:10
  • @Karl- yes my connection string is correct and the message in the sqlException is **An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: Login failed for user ''.** – larie Dec 05 '13 at 03:15
  • @Karl - there is alos this message **Debugger:Exception Intercepted: ld, Form1.vb line 32 An exception was intercepted and the call stack unwound to the point before the call from user code where the exception occurred. "Unwind the call stack on unhandled exceptions" is selected in the debugger options.** – larie Dec 05 '13 at 03:18

1 Answers1

1

your connection string is missing necessary information ... you have to either set IntegratedSecurity to true (and make sure that your windows user has permissions to access the database) or provide sql user name and password ... I would suggest to use SqlConnectionStringBuilder to create a valid sql connection string

PrfctByDsgn
  • 1,022
  • 1
  • 14
  • 18
  • @ PrfctByDsgn - ok thank you i will try and i will let u know if it working because before i have used integrity security and Initial catalog but it didnt work either.....so i guess i will go for sqlconnectionStringBuilder,,,or maybe because i'm using windows 8 – larie Dec 05 '13 at 10:11