what is the problem with this code

shahzadhard 0 Tallied Votes 387 Views Share

Imports System.Data
Imports System.Data.SqlClient

Public Class Form2
Dim myconnection As SqlConnection
Dim mycommand As SqlCommand
Dim dr As SqlDataReader
Dim dr1 As SqlDataReader
Dim ra As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myconnection = New SqlConnection("server=Pakistan;Initial Catalog=Mumtaz;database=Mumtaz")


myconnection.Open()
mycommand = New SqlCommand("insert into temp([name],[datep]) values ('" & TextBox1.Text & "','" & TextBox2.Text & "')", myconnection)
mycommand.ExecuteNonQuery()
'MessageBox.Show("New Row Inserted" & ra)
myconnection.Close()
End Sub
End Class

i am using vb.net 2005 and sql server 2000 i want insert data using textboxes 
and when i press add button error comes on myconnection.open() point 
what is going on


Imports System.Data
Imports System.Data.SqlClient

Public Class Form2
    Dim myconnection As SqlConnection
    Dim mycommand As SqlCommand
    Dim dr As SqlDataReader
    Dim dr1 As SqlDataReader
    Dim ra As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        myconnection = New SqlConnection("server=Pakistan;Initial Catalog=Mumtaz;database=Mumtaz")


        myconnection.Open()
        mycommand = New SqlCommand("insert into temp([name],[datep]) values ('" & TextBox1.Text & "','" & TextBox2.Text & "')", myconnection)
        mycommand.ExecuteNonQuery()
        'MessageBox.Show("New Row Inserted" & ra)
        myconnection.Close()
    End Sub
End Class
TomW 73 Posting Whiz

The problem is in your connection statement. Your specifying the database twice, each a different way and your not specifying the security.

shahzadhard 0 Newbie Poster

so what is correct code

nevintom26 0 Newbie Poster

in the 16th line u should use
myconnection = New SqlConnection("server=Pakistan;server=Mumtaz;integrated security= true")
'if u use a password and username to your database u should use
'myconnection = New SqlConnection("server=Pakistan;server=Mumtaz;user=username;password=password")
'here use ur username and password insted of username and password

Be a part of the DaniWeb community

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