Create Record in Database.

Discussion in 'ASP .Net' started by Shapper, Jun 30, 2005.

  1. Shapper

    Shapper Guest

    Hello,

    I need to create a new record in a database.
    The database has 3 fields: [id] (autonumber), [title] and [text]
    (strings)

    When I create the record how should I create the [id] value?

    Does the database insert it automatically?

    Do I need to create a random number?
    How can I do it to not repeat the id's already exist in the table?

    My code is not working but it seems fine to me:

    Dim connectionString As String =
    System.Configuration.ConfigurationSettings.AppSettings("connectionString")
    Dim dbConnection As System.Data.IDbConnection = New
    System.Data.OleDb.OleDbConnection(connectionString)

    Dim queryString As String = "INSERT INTO [t_news] ([title], [text])
    VALUES (@title, @text)"
    Dim dbCommand As System.Data.IDbCommand = New
    System.Data.OleDb.OleDbCommand
    dbCommand.CommandText = queryString
    dbCommand.Connection = dbConnection

    Dim dbParam_title As System.Data.IDataParameter = New
    System.Data.OleDb.OleDbParameter
    dbParam_title.ParameterName = "@title"
    dbParam_title.Value = title
    dbParam_title.DbType = System.Data.DbType.String
    dbCommand.Parameters.Add(dbParam_title)

    Dim dbParam_text As System.Data.IDataParameter = New
    System.Data.OleDb.OleDbParameter
    dbParam_text.ParameterName = "@text"
    dbParam_text.Value = text
    dbParam_text.DbType = System.Data.DbType.String
    dbCommand.Parameters.Add(dbParam_text)

    Dim rowsAffected As Integer = 0
    dbConnection.Open
    Try
    rowsAffected = dbCommand.ExecuteNonQuery
    Finally
    dbConnection.Close
    End Try

    Thanks,
    Miguel
    Shapper, Jun 30, 2005
    #1
    1. Advertising

  2. Shapper

    Karl Seguin Guest

    Miguel:
    your connection.Open() should be in the try but that isn't your problem.
    The id should be automatically generated. What isn't working? Are you
    getting an error? is the row simply not getting added? Is this access?

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)


    "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
    news:...
    > Hello,
    >
    > I need to create a new record in a database.
    > The database has 3 fields: [id] (autonumber), [title] and [text] (strings)
    >
    > When I create the record how should I create the [id] value?
    >
    > Does the database insert it automatically?
    >
    > Do I need to create a random number?
    > How can I do it to not repeat the id's already exist in the table?
    >
    > My code is not working but it seems fine to me:
    >
    > Dim connectionString As String =
    > System.Configuration.ConfigurationSettings.AppSettings("connectionString")
    > Dim dbConnection As System.Data.IDbConnection = New
    > System.Data.OleDb.OleDbConnection(connectionString)
    >
    > Dim queryString As String = "INSERT INTO [t_news] ([title], [text])
    > VALUES (@title, @text)"
    > Dim dbCommand As System.Data.IDbCommand = New
    > System.Data.OleDb.OleDbCommand
    > dbCommand.CommandText = queryString
    > dbCommand.Connection = dbConnection
    >
    > Dim dbParam_title As System.Data.IDataParameter = New
    > System.Data.OleDb.OleDbParameter
    > dbParam_title.ParameterName = "@title"
    > dbParam_title.Value = title
    > dbParam_title.DbType = System.Data.DbType.String
    > dbCommand.Parameters.Add(dbParam_title)
    >
    > Dim dbParam_text As System.Data.IDataParameter = New
    > System.Data.OleDb.OleDbParameter
    > dbParam_text.ParameterName = "@text"
    > dbParam_text.Value = text
    > dbParam_text.DbType = System.Data.DbType.String
    > dbCommand.Parameters.Add(dbParam_text)
    >
    > Dim rowsAffected As Integer = 0
    > dbConnection.Open
    > Try
    > rowsAffected = dbCommand.ExecuteNonQuery
    > Finally
    > dbConnection.Close
    > End Try
    >
    > Thanks,
    > Miguel
    >
    Karl Seguin, Jun 30, 2005
    #2
    1. Advertising

  3. Shapper

    Shapper Guest

    It seems I add an answer to this post but my newsreader says "This
    article is no longer available on the server".

    Why is that?

    Thanks,
    Miguel

    "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
    news:mdmoura*NOSPAM*@gmail.*DELETE2SEND*com:

    > Hello,
    >
    > I need to create a new record in a database.
    > The database has 3 fields: [id] (autonumber), [title] and [text]
    > (strings)
    >
    > When I create the record how should I create the [id] value?
    >
    > Does the database insert it automatically?
    >
    > Do I need to create a random number?
    > How can I do it to not repeat the id's already exist in the table?
    >
    > My code is not working but it seems fine to me:
    >
    > Dim connectionString As String =
    > System.Configuration.ConfigurationSettings.AppSettings("connectionString")
    > Dim dbConnection As System.Data.IDbConnection = New
    > System.Data.OleDb.OleDbConnection(connectionString)
    >
    > Dim queryString As String = "INSERT INTO [t_news] ([title], [text])
    > VALUES (@title, @text)"
    > Dim dbCommand As System.Data.IDbCommand = New
    > System.Data.OleDb.OleDbCommand
    > dbCommand.CommandText = queryString
    > dbCommand.Connection = dbConnection
    >
    > Dim dbParam_title As System.Data.IDataParameter = New
    > System.Data.OleDb.OleDbParameter
    > dbParam_title.ParameterName = "@title"
    > dbParam_title.Value = title
    > dbParam_title.DbType = System.Data.DbType.String
    > dbCommand.Parameters.Add(dbParam_title)
    >
    > Dim dbParam_text As System.Data.IDataParameter = New
    > System.Data.OleDb.OleDbParameter
    > dbParam_text.ParameterName = "@text"
    > dbParam_text.Value = text
    > dbParam_text.DbType = System.Data.DbType.String
    > dbCommand.Parameters.Add(dbParam_text)
    >
    > Dim rowsAffected As Integer = 0
    > dbConnection.Open
    > Try
    > rowsAffected = dbCommand.ExecuteNonQuery
    > Finally
    > dbConnection.Close
    > End Try
    >
    > Thanks,
    > Miguel
    Shapper, Jun 30, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVzdGlu?=

    Retrieving Record Key while creating the record.

    =?Utf-8?B?SnVzdGlu?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    615
    =?Utf-8?B?SnVzdGlu?=
    Oct 5, 2004
  2. André
    Replies:
    0
    Views:
    3,729
    André
    Jun 25, 2006
  3. MRW
    Replies:
    0
    Views:
    364
  4. THurkmans
    Replies:
    2
    Views:
    587
  5. Frits van Soldt

    How to insert a new record in detailsview when there are no record

    Frits van Soldt, Nov 16, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    719
    Frits van Soldt
    Nov 17, 2005
Loading...

Share This Page