Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Create Record in Database.

Reply
Thread Tools

Create Record in Database.

 
 
Shapper
Guest
Posts: n/a
 
      06-30-2005
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.AppSett ings("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

 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      06-30-2005
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:(E-Mail Removed)...
> 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.AppSett ings("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
>



 
Reply With Quote
 
 
 
 
Shapper
Guest
Posts: n/a
 
      06-30-2005
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.AppSett ings("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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating a Record using a current record MRW ASP .Net 0 02-11-2008 06:58 PM
IP Address, MX Record, A Record Question K.J. 44 Cisco 2 09-06-2006 05:14 PM
You cannot add or change a record because a related record is required in table 'lok" André ASP .Net 0 06-25-2006 01:30 PM
Retrieving Record Key while creating the record. =?Utf-8?B?SnVzdGlu?= ASP .Net 4 10-05-2004 08:11 PM
" Invalid Disk Table in Boot Record - Boot Record could not be repaired " reply@newsgroup.please Computer Support 2 12-01-2003 05:37 AM



Advertisments