i am new to programing help me out

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • naveens
    New Member
    • Jun 2008
    • 1

    #1

    i am new to programing help me out

    I want to insert values from vb textbox in sql table named [home] and my code is


    Private Sub Button1_Click_1 (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
    Dim result As Integer
    Dim conn As New OleDb.OleDbConn ection("provide r=SQLOLEDB;User ID=sa;password= xxxxxx;Initial Catalog=first;d ata source=NAVEEN;" )
    conn.Open()
    Dim insert1 As New OleDb.OleDbComm and
    insert1 = New OleDb.OleDbComm and("insert into home values('" + TextBox1.Text + "','" + TextBox2.Text + "'),conn")
    insert1.Command Type = CommandType.Tex t
    result = insert1.Execute NonQuery()
    conn.Close()

    error message that i get is

    ExecuteNonQuery : Connection property has not been initialized.


    please help out

    thanks
  • jamesd0142
    Contributor
    • Sep 2007
    • 471

    #2
    Imports System.Data.Ole Db

    [code=vbnet]
    Try
    Dim StrConn As String = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=c:\Users \Users.mdb"
    Dim oConn As New System.Data.Ole Db.OleDbConnect ion(StrConn)
    Dim con As New System.Data.Ole Db.OleDbCommand
    con.Connection = oConn
    con.CommandType = CommandType.Tex t
    con.CommandText = ""
    oConn.Open()
    con.ExecuteNonQ uery()
    oConn.Close()
    Catch
    End Try
    [/code]

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Part of your problem seems to be that you have a quote after conn. But let me show you an easier way to build your command string:
      Code:
      Dim cmdStr As String
      cmdStr = String.Format("insert into homes values('{0}','{1}')", TextBox1.Text, TextBox2.Text)
      Dim insert1 As New OleDb.OleDbCommand
      insert1 = New OleDb.OleDbCommand(cmdStr,conn)
      String.Format will replace the {#} with the parameters that follow in order, so {0} will have the value of TextBox1.Text, and {1} will have the value of TextBox2.Text.

      Also, I'd suggest that instead of OleDb, you use SqlClient. SqlClient is the native .NET data provider for MS SQL Server. So OleDbConnection becomes SqlConnection (get your connection string here) and OleDbCommand becomes SqlCommand.

      Comment

      Working...