M
MyCatAlex
Guest
I have this piece of code:
private string saveNewItemInOleDb ( string parentNumeric_id, char childsChar )
{
string numeric_id = "";
string dataTableName = String.Empty;
string textToInsert = String.Empty;
DoServer.getSrvConnOrSwitch ( "ComeAndGetThr" );
using ( SqlConnection conn = new SqlConnection ( Globals.srv.ConnectionContext.ConnectionString ) )
{
dataTableName = getAliasFromFirstChar ( childsChar );
StringBuilder sb = new StringBuilder ( "SELECT * FROM " + dataTableName +
" WHERE numeric_id = (SELECT MAX (numeric_id) FROM " + dataTableName + ")" );
string comndText = sb.ToString ( );
DateTime dated = this.dateTimePickerDiaries.Value;
string cDated = dated.ToString ( "MM/dd/yyyy hh:mm:ss" );
conn.Open ( );
SqlCommand cmdm = new SqlCommand ( );
cmdm.Connection = conn;
cmdm.CommandType = CommandType.Text;
cmdm.CommandText = comndText;
StringBuilder sbTemp = new StringBuilder();
using ( SqlDataReader rdr = cmdm.ExecuteReader ( CommandBehavior.CloseConnection ) )
{
if ( rdr.HasRows == true )
{
foreach (System.Data.Common.DbDataRecord row in rdr)
{
if (parentNumeric_id != "Node0")
{
numeric_id = (string)row[0];
numeric_id = makeNumeric_id(numeric_id);
}
else
{ // this is the case when the new item (category) is entered in the very first column
rdr.Close();
StringBuilder sb22 = new StringBuilder();
numeric_id = "0000000";
string memo101 = " ";
DateTime dated101 = DateTime.Now;
sb22.Append("INSERT INTO dbo.categories (numeric_id, named, dated, memo1 ) " +
" VALUES ( @numeric_id, @named, @dated101, @memo101"); // <<=== EXCEPTION HERE
SqlCommand cmdm101 = new SqlCommand();
cmdm101.Connection = conn;
cmdm101.CommandType = CommandType.Text;
cmdm101.CommandText = sb22.ToString();
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
try
{
cmdm101.ExecuteNonQuery();
}
catch (Exception ex)
{
TSP.TextToSpeech("could not save item because of exception");
Console.WriteLine(" {0} \r\n {1} ", ex.Message, ex.StackTrace);
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
return String.Empty;
}
}
}
}
if ( rdr.IsClosed == false )
{
rdr.Close ( );
}
}
The Exception:
Must declare the scalar variable "@numeric_id".
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ComeAndGet.Form1.saveNewItemInOleDb(String parentNumeric_id, Char childsChar) in C:\VCSharp_Projects\ComeAndGet\ComeAndGet\Form1.cs:line 2026
It is a part of a very large program which I completed around 8 years ago. Since then I haven't touched it for a few years. Whilst checking the code now I found that @numeric_id parameter is used in many sections of the program and I could not see any definitions anywhere. I wonder what is going on? I tried to define it as a string which is what it is and got compile errors as a result.
Thanks.
Continue reading...
private string saveNewItemInOleDb ( string parentNumeric_id, char childsChar )
{
string numeric_id = "";
string dataTableName = String.Empty;
string textToInsert = String.Empty;
DoServer.getSrvConnOrSwitch ( "ComeAndGetThr" );
using ( SqlConnection conn = new SqlConnection ( Globals.srv.ConnectionContext.ConnectionString ) )
{
dataTableName = getAliasFromFirstChar ( childsChar );
StringBuilder sb = new StringBuilder ( "SELECT * FROM " + dataTableName +
" WHERE numeric_id = (SELECT MAX (numeric_id) FROM " + dataTableName + ")" );
string comndText = sb.ToString ( );
DateTime dated = this.dateTimePickerDiaries.Value;
string cDated = dated.ToString ( "MM/dd/yyyy hh:mm:ss" );
conn.Open ( );
SqlCommand cmdm = new SqlCommand ( );
cmdm.Connection = conn;
cmdm.CommandType = CommandType.Text;
cmdm.CommandText = comndText;
StringBuilder sbTemp = new StringBuilder();
using ( SqlDataReader rdr = cmdm.ExecuteReader ( CommandBehavior.CloseConnection ) )
{
if ( rdr.HasRows == true )
{
foreach (System.Data.Common.DbDataRecord row in rdr)
{
if (parentNumeric_id != "Node0")
{
numeric_id = (string)row[0];
numeric_id = makeNumeric_id(numeric_id);
}
else
{ // this is the case when the new item (category) is entered in the very first column
rdr.Close();
StringBuilder sb22 = new StringBuilder();
numeric_id = "0000000";
string memo101 = " ";
DateTime dated101 = DateTime.Now;
sb22.Append("INSERT INTO dbo.categories (numeric_id, named, dated, memo1 ) " +
" VALUES ( @numeric_id, @named, @dated101, @memo101"); // <<=== EXCEPTION HERE
SqlCommand cmdm101 = new SqlCommand();
cmdm101.Connection = conn;
cmdm101.CommandType = CommandType.Text;
cmdm101.CommandText = sb22.ToString();
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
try
{
cmdm101.ExecuteNonQuery();
}
catch (Exception ex)
{
TSP.TextToSpeech("could not save item because of exception");
Console.WriteLine(" {0} \r\n {1} ", ex.Message, ex.StackTrace);
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
return String.Empty;
}
}
}
}
if ( rdr.IsClosed == false )
{
rdr.Close ( );
}
}
The Exception:
Must declare the scalar variable "@numeric_id".
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ComeAndGet.Form1.saveNewItemInOleDb(String parentNumeric_id, Char childsChar) in C:\VCSharp_Projects\ComeAndGet\ComeAndGet\Form1.cs:line 2026
It is a part of a very large program which I completed around 8 years ago. Since then I haven't touched it for a few years. Whilst checking the code now I found that @numeric_id parameter is used in many sections of the program and I could not see any definitions anywhere. I wonder what is going on? I tried to define it as a string which is what it is and got compile errors as a result.
Thanks.
Continue reading...