1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help Me

Discussion in 'C#' started by bestwriter, Jul 28, 2007.

  1. #1
    Q1. Please see this code below:

    Dim cn As New SqlClient.SqlConnection
    Dim cm As New SqlClient.SqlCommand

    'Now this part is in form load'

    Dim s As String
    s = "data source=(local); uid=sa; pwd=sa; initial catalog=Salon"
    cn = New SqlClient.SqlConnection(s)
    cn.Open()

    'Now this part is on click event of button'

    cm.CommandText = "Insert into Clients values('" & Me.TextBox1.Text & "','" & Me.TextBox2.Text & "','" & Me.TextBox3.Text & "','" & Me.TextBox4.Text & "','" & Me.TextBox5.Text & "','" & Me.TextBox6.Text & "','" & Me.TextBox7.Text & "','" & Me.TextBox8.Text & "','" & Me.TextBox9.Text & "','" & Me.DateTimePicker1.Value & "')"
    cm.Connection = cn
    cm.ExecuteNonQuery()
    cn.Close()

    Now this insert query can insert values in all columns but it cant insert values in primary key and also cant insert pictures in sql database. So please guide me how to handle botht these problems. Send me the source code if possible.
     
    bestwriter, Jul 28, 2007 IP
  2. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #2
    your primary key is probably set as an Auto-Increment field which is NOT insertable into (It increases by X every time automatically)... When doing inserts do not include this field to insert into.

    Make sure your datatype is Image when inserting pictures. Load the image into byte() (byte array) and insert the blob.
     
    ccoonen, Jul 31, 2007 IP
  3. dizzy

    dizzy Peon

    Messages:
    43
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ccoonen is right.

    If you need the Primary key that's inserted, you probably have to move your INSERT statement into a stored procedure.

    In the stored procedure you can use "SELECT @@IDENTITY AS ClientId"
    after your insert statement to retrieve the autoincrement Id that was just added.
     
    dizzy, Jul 31, 2007 IP
  4. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #4
    A more reliable way to get the identity created is to use:

    SELECT SCOPE_IDENTITY() but @@Identity is fine too :)
     
    ccoonen, Aug 2, 2007 IP
  5. shrkscn

    shrkscn Peon

    Messages:
    1,157
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hey the fun is in trying out and experimenting. You will learn a lot experimenting rather than putting in code developed by some one else. :p

     
    shrkscn, Aug 4, 2007 IP