简体   繁体   English

Visual Studio错误无法在int上调用方法

[英]Visual Studio error cannot call method on int

Just Trying to insert some simple data into a table but every time I try I get the error "cannot call method on int". 只是试图将一些简单的数据插入表中,但是每次尝试时,都会出现错误“无法在int上调用方法”。 Here is my code, thanks for any help data types in table are int for cinema Id and nvarchar(50) for ReviewText 这是我的代码,感谢表中的任何帮助数据类型对于Cinema Id为int,对于ReviewText为nvarchar(50)

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. 无法在int上调用方法。

Description: 描述:

An unhandled exception occurred during the execution of the current web request. 当前Web请求的执行期间发生未处理的异常。 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. System.Data.SqlClient.SqlException:无法在int上调用方法。

Source Error: 源错误:

An unhandled exception was generated during the execution of the current web request. 当前Web请求的执行期间生成了未处理的异常。 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.] [SqlException(0x80131904):无法在int上调用方法。
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction) +1767866
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔值breakConnection,操作1 wrapCloseInAction) +1767866
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction) +1767866
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 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
1 wrapCloseInAction) +1767866
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction)+5352418 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用者HasConnectionLock,布尔asyncClose)+244

System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691 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.TryConsumeMetaData()+61
System.Data.SqlClient.SqlDataReader.get_MetaData() +90 System.Data.SqlClient.SqlDataReader.get_MetaData()+90
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +365 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,字符串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.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean异步,Int32超时,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方法,TaskCompletionSource`1完成,Int32超时,Task&task,布尔asyncWrite)+177
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法)+53
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +134 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior行为,字符串方法)+134
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +41 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior行为)+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.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior行为)+10 System.Data.Common.DbDataAdapter.FillInternal(DataSet数据集,DataTable []数据表,Int32 startRecord,Int32 maxRecords,字符串srcTable,IDbCommand命令,CommandBehavior行为)+140
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +316 System.Data.Common.DbDataAdapter.Fill(数据集dataSet,Int32 startRecord,Int32 maxRecords,字符串srcTable,IDbCommand命令,CommandBehavior行为)+316

You don't need to qualify the column names in your INSERT statement with the table name. 您不需要使用表名来限定INSERT语句中的列名。 Use this as your SQL: 使用它作为您的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. 另外,如果要在application / web.config中查找连接字符串,则不能仅将键放在SqlConnection构造函数中以获取实际的连接字符串。 You have to use the ConfigurationManager class to get value, which would look something like this: 您必须使用ConfigurationManager类来获取值,看起来像这样:

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

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: 也强烈建议通过这种方式使用SqlConnection:

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();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM