Friday, February 24, 2012

Exception - Insert Record into DB

Hello

I'm using express edition to create my trail testapplication. Below is the code that I have and I'm trying to insert data to thedatabase table named "Coin".

ProtectedSubbtnSave_Click(ByVal senderAsObject,ByVal eAsSystem.EventArgs)Handles btnview.Click

Dim sAsString = txtCname.Text

'Dim myConnection As NewSqlConnection(myConnString)

Dim descAsString = txtCDesc.Text

Dim ConStrAsNew SqlClient.SqlConnection

ConStr.ConnectionString ="server=test\sqlinstance;Integrated Security=True"

Response.Write("Connection string: " & ConStr.ConnectionString)

Try

Dim SelectQueryAsString ="SELECTmax(coinid) from coin"

Dim idvalAsInteger = 0

Dim commandAsNew SqlCommand(SelectQuery, ConStr)

ConStr.Open()

idval = command.ExecuteScalar()

Console.WriteLine(idval)

idval = idval + 1

Dim InsertQueryAsString ="INSERTINTO COIN(coinname, coinid, ebayid, ebaymember, ebaymemid , amount , coindesc)VALUES('1992-Proof'," & idval &",'eewerwer','sp6937','serwryana',67.70,'MattProff of 1992- Mint Set')"

Dim command1AsNew SqlCommand(InsertQuery, ConStr)

command1.ExecuteScalar()

Dim S1AsString ="Recordinsert - Successful!"

Console.WriteLine(S1)

Catch exAsException

Label2.Text = ex.ToString

Finally

ConStr.Close()

EndTry

End Sub

This program isthrowing an exception (mentioned below)

Exception --> System.Data.SqlClient.SqlException:Invalid object name 'coin'. atSystem.Data.SqlClient.SqlConnection.OnError(SqlException exception, BooleanbreakConnection) atSystem.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception,Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObjectstateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSetbulkCopyHandler, TdsParserStateObject stateObj) atSystem.Data.SqlClient.SqlDataReader.ConsumeMetaData() atSystem.Data.SqlClient.SqlDataReader.get_MetaData() atSystem.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehaviorcmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) atSystem.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResultresult) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehaviorcmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) atSystem.Data.SqlClient.SqlCommand.ExecuteScalar() at _Default.btnview_Click(Objectsender, EventArgs e) in C:\Documents and Settings\arsha\My Documents\VisualStudio 2005\WebSites\WebSite1\Default.aspx.vb:line 92

"Coin" is the table name – which is in SQL server. Kindlyhelp me to handle and overcome this exception.

Thanks

perhaps you have set up your database with a case sensitive collation?

Try altering your sql statement so it's case exactly matches your table.

also, i would strongly recommend that you let sql server take care of incrementing the CoinId data by using an identity column. By trying to do it yourself, if 2 people run your page simultaneously, you could end up with a problem.

|||

Try using ExecuteNonQuery instead ofExecuteScalar

No comments:

Post a Comment