htm11h
htm11h

Reputation: 1779

parameterized insert query not working

I am trying to run what I thought was a rather simple parameterized insert query, but am running into all sorts of problems. SQL EXPRESS 08R2, VB.net

Here is the VB code that builds the parameters... UPDATED Variable names:

itemDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ToString()
itemDataSource1.InsertCommandType = SqlDataSourceCommandType.StoredProcedure
itemDataSource1.InsertParameters.Add("@short_Text", short_Text) 
itemDataSource1.InsertParameters.Add("@foreignTable", foreignTable)
itemDataSource1.InsertCommand = "usp_insertLangShortText"         ' Call stored procedure

'***********************************************************************
 ' Begin the insertion. Check for errors during insert process.

Dim rowsAffected As Integer = 0
rowsAffected = itemDataSource1.Insert()

And here is the SP that I am trying to perform the insert with..

@short_Text varchar(250), 
@foreignTable varchar(250)

AS
/* SET NOCOUNT ON */
DECLARE @sql varchar(max);

   BEGIN
SET @sql =  'INSERT INTO ' + @foreignTable + ' (short_Text) VALUES (''' + @short_Text + ''')';
   END

print (@sql) 
EXECUTE(@sql); 
SET @sql = '' 

RETURN

ANY suggestions on how to code this more intelligently are welcome.

Here is the error.... System.Data.SqlClient.SqlException: Procedure or function usp_insertLangShortText has too many arguments specified. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) at System.Web.UI.WebControls.SqlDataSourceView.ExecuteInsert(IDictionary values) at System.Web.UI.WebControls.SqlDataSource.Insert() at par_importExcelDataPTS.GetRootID(String short_Text, Int32 string_id, String dbName, String Lang_code) in C:\Users

Upvotes: 0

Views: 982

Answers (2)

competent_tech
competent_tech

Reputation: 44931

I believe that you may need to add an @ in front of the parameter names when you add them:

itemDataSource1.InsertParameters.Add("@short_Text", short_Text) 
itemDataSource1.InsertParameters.Add("@foreignTable", foreignTable)

Also, if your short_text contains any single quotes, the SQL will fail to evaluate. To resolve this, replace all single quotes in short_Text with two single quotes.

If this does not resolve your issue, can you post the exact error messages that you are receiving?

Upvotes: 0

Gary.S
Gary.S

Reputation: 7121

Is there a specific reason the parameter names are different as this may be causing your issue:

c# - itemDataSource1.InsertParameters.Add("short_Text", short_Text)

SQL - @shrtText varchar(250)

Should it not be: itemDataSource1.InsertParameters.Add("shrtText", short_Text)

Upvotes: 2

Related Questions