繁体   English   中英

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

[英]Visual Studio error cannot call method on int

只是试图将一些简单的数据插入表中,但是每次尝试时,都会出现错误“无法在int上调用方法”。 这是我的代码,感谢表中的任何帮助数据类型对于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());
        }

显示的错误:

无法在int上调用方法。

描述:

当前Web请求的执行期间发生未处理的异常。 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

异常详细信息:

System.Data.SqlClient.SqlException:无法在int上调用方法。

源错误:

当前Web请求的执行期间生成了未处理的异常。 可以使用下面的异常堆栈跟踪来标识有关异常的来源和位置的信息。

堆栈跟踪:

[SqlException(0x80131904):无法在int上调用方法。
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,布尔调用者HasConnectionLock,布尔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,字符串resetOptionsString)+365
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方法,TaskCompletionSource`1完成,Int32超时,Task&task,布尔asyncWrite)+177
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法)+53
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior行为,字符串方法)+134
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior行为)+41
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,Int32 startRecord,Int32 maxRecords,字符串srcTable,IDbCommand命令,CommandBehavior行为)+316

您不需要使用表名来限定INSERT语句中的列名。 使用它作为您的SQL:

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

另外,如果要在application / web.config中查找连接字符串,则不能仅将键放在SqlConnection构造函数中以获取实际的连接字符串。 您必须使用ConfigurationManager类来获取值,看起来像这样:

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

您的代码有问题,我不知道此问题是否是您异常的根源,请首先对其进行编辑,然后告诉我们结果:

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

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