Click here to Skip to main content
15,891,597 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to add data in a sql server table from vb.net applicattion pls help me,

my code block is-:
VB
Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlConnection

Public Class Form1
    Private cn As SqlConnection
    Dim constr As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        constr = "Integrated Security=SSPI;Initial Catalog=anand;Data Source=ANAND-PC\SQLEXPRESS;"

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cmd As New SqlClient.SqlCommand
        Dim query As String
        query = "Insert Into anand1 (roll) values (" & TextBox1.Text & ");"

        cmd.CommandText = query
        cmd.CommandType = CommandType.Text
        cmd.Connection = cn

        cn.Open()

        cmd.ExecuteNonQuery()

        cn.Close()

    End Sub
Posted
Updated 16-Apr-12 22:58pm
v2

hey

refer following link it will help u to connect with sqlserver
http://www.developerfusion.com/article/4278/using-adonet-with-sql-server/[^]
Best Luck
happy Coding
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 
Use
VB
cmd.CommandText = query
 cmd.CommandType = CommandType.Text
cn = New SqlConnection(constr)
cn.Open()
cmd.Connection = cn

and check your connection string
http://www.connectionstrings.com/[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900