2
votes

Just Trying to insert some simple data into a table but every time I try I get the error "cannot call method on int". Here is my code, thanks for any help data types in table are int for cinema Id and nvarchar(50) for ReviewText

protected void BtnSubmitReview_Click(object sender, EventArgs e)
    {
        try
        {           
            System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("RegistrationConnectionString");

            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.CommandType = System.Data.CommandType.Text;

            cmd.CommandText = "INSERT INTO ReviewText (ReviewText.CinemaID, ReviewText.ReviewText) VALUES (1, 'NorthWestern')";
            cmd.Connection = sqlConnection1;

            sqlConnection1.Open();
            cmd.ExecuteNonQuery();
            sqlConnection1.Close();

            Response.Write("Your Review was saved");                
        }
        catch (Exception ex)
        {
            Response.Write("Error" + ex.ToString());
        }

Error Shown:

Cannot call methods on int.

Description:

An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:

System.Data.SqlClient.SqlException: Cannot call methods on int.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[SqlException (0x80131904): Cannot call methods on int.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) +1767866
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction) +5352418 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691
System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61
System.Data.SqlClient.SqlDataReader.get_MetaData() +90
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +365
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) +1406
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +177
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +134
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41
System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +140
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +316

2
Can you post the actual error/stacktrace?Martin Costello
Are you putting an actual connection string in the SqlConnection constructor or putting in "RegistrationConnectionString" ?Cam Bruce
ResgistrationConnectionString is a connection i was using on another page for inserting other data into the database , just using it again instead of creating new connectionuser3542214
The Problem was that I was using the name of the table for the name of one of the columns , The query must get mixed up if you do so, thanksuser3542214
@user3542214 I answered your question, mentioning the problem, but I get -1 :(((Reza

2 Answers

0
votes

You don't need to qualify the column names in your INSERT statement with the table name. Use this as your SQL:

INSERT INTO ReviewText (CinemaID, ReviewText) VALUES (1, 'NorthWestern')

Also, If you are looking for a connection string in your application/web.config you can't just put the key in the SqlConnection constructor to get the actual connection string. You have to use the ConfigurationManager class to get value, which would look something like this:

var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
-2
votes

Your code has a problem and I don't know if this problem is the root of your exception or not, please first edit it to this and then tell us the result:

    //you dont need to repeat table name in insert command's columns
    cmd.CommandText = "INSERT INTO ReviewText (CinemaID, ReviewText) VALUES (1, 'NorthWestern')";

also it is highly recommended to use SqlConnection via this way:

using(System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection("RegistrationConnectionString"))
{
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
        cmd.CommandType = System.Data.CommandType.Text;

        cmd.CommandText = "INSERT INTO ReviewText (CinemaID, ReviewText) VALUES (1, 'NorthWestern')";
        cmd.Connection = sqlConnection1;

        sqlConnection1.Open();
        cmd.ExecuteNonQuery();
        sqlConnection1.Close();

}