Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将当前项目id添加到sitecore中的数据库中_C#_.net_Sitecore_Sitecore6_Sqlclient - Fatal编程技术网

C# 将当前项目id添加到sitecore中的数据库中

C# 将当前项目id添加到sitecore中的数据库中,c#,.net,sitecore,sitecore6,sqlclient,C#,.net,Sitecore,Sitecore6,Sqlclient,我的数据库中有ProductID列,我想将当前项目id添加到此列。我试过这个,但不起作用: private void InsertInfo() { SqlConnection conn = new SqlConnection(GetConnectionString()); string sql = "INSERT INTO CommentsTable (Name, Email, Comment, ProductID) VALUES (@Name,@Ema

我的数据库中有ProductID列,我想将当前项目id添加到此列。我试过这个,但不起作用:

private void InsertInfo()
    {

        SqlConnection conn = new SqlConnection(GetConnectionString());
        string sql = "INSERT INTO CommentsTable (Name, Email, Comment, ProductID) VALUES (@Name,@Email,@Comment,@ProductID)";
        try
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
            cmd.Parameters.AddWithValue("@Email", TextBox2.Text);
            cmd.Parameters.AddWithValue("@Comment", TextBox3.Text);
            cmd.Parameters.AddWithValue("@ProductID", Sitecore.Context.Item.ID.ToString());
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }

提前感谢。

如果您要查找当前正在浏览的产品的ID,可以使用:
Sitecore.Context.Item.ID
。如果你不能使用它,你必须确保你的querystring确实存在并且有一个值。对于Sitecore,您可以使用:
Sitecore.Web.WebUtil.GetQueryString(“id”,string.Empty)
其中
string.Empty
可以替换为您想要的默认值。

任何异常或错误消息?System.ArgumentNullException:值不能为null。在哪一行?请调试您的代码。var currentItem=Sitecore.Context.Database.GetItem(Request.QueryString[“id”])上下文、数据库、请求或QueryString为空或id不是QueryString中的键。我已尝试Sitecore.Context.Item.id(上面的代码),但是出现了异常:System.ArgumentException:不存在从对象类型Sitecore.Data.ID到已知托管提供程序本机类型的映射。您可能需要使用Sitecore.Context.Item.ID.ToString()。虽然ID是一个GUID,但它取决于您试图将其保存到表中的格式。我想说的是,您必须仔细检查CommentsTable定义,以确保ProductID列中没有任何键入错误-这就是错误听起来的样子…它工作正常(当您在浏览器中浏览此项目时),但它在自定义编辑器选项卡(在sitecore内容编辑器中)中不起作用。我在content editor中创建了一个自定义编辑器选项卡,并在URL中使用相同的代码选择了aspx布局。在一个选项卡中,它显示异常:对象引用未设置为对象的实例。我猜在内容编辑器中,sitecore不理解什么是“sitecore.Context.Item.ID”。如何解决这个问题?