【问题标题】:SQL Server Error, 'Keyword not supported 'datasource'SQL Server 错误,'关键字不支持'数据源'
【发布时间】:2013-02-18 02:40:27
【问题描述】:

我目前正在尝试将一些值插入数据源,然后使用 DataList 控件将它们显示在另一个页面上。但是,经过一些测试和实验,我发现错误从一开始就出现了。

这是我绑定到按钮的代码。

protected void btnSend_Click(object sender, EventArgs e)
    {
    Page.Validate("vld2");
    SendMail();
    lblMsgSend.Visible = true;
    txtPhone.Text = "";
    txtEmail.Text = "";
    txtName.Text = "";
    txtComment.Text = "";

    //SQL Server Database
    SqlConnection conn; //manages connection to database
    SqlCommand cmd; //manages the SQL statements
    string strInsert; //SQL INSERT Statement
        try
        {
            //create a connection object
            conn = new SqlConnection("DataSource=localhost\\sqlexpress;" +
                                     "Initial Catalog=RionServer;" +
                                     "Integrated Security=True;");
            //Build the SQL INSERT Document
            strInsert = "INSERT INTO CommentsAdmin (Name,Phone,Email,Comments)"
                + "VALUES(@Name,@Phone,@Email,@Comments);";
            //associate the INSERT statement with the connection
            cmd = new SqlCommand(strInsert, conn);
            //TELL the SqlCommand WHERE to get the data from
            cmd.Parameters.AddWithValue("Name", txtName);
            cmd.Parameters.AddWithValue("Phone", txtPhone);
            cmd.Parameters.AddWithValue("Email", txtEmail);
            cmd.Parameters.AddWithValue("Comments", txtComment);
            //open the connection
            cmd.Connection.Open();
            //run the SQL statement
            cmd.ExecuteNonQuery();
            //close connection
            cmd.Connection.Close();
            //display status message on the webpage
            lblMsgSend.Text = "Thank you for the comment! Please hit the 'Return to Main Page' to return to the Main Page!";
        }
        catch (Exception ex)
        {
            lblMsgSend.Text = ex.Message;
        }
    }

这是我的网页图片及其显示的错误。

如果您需要更多信息,请告诉我。

提前致谢。

【问题讨论】:

  • cmd.Parameters.AddWithValue("@Name", txtName);.我想你错过了@。
  • -1 Shree 你在说什么.. OP 在他的标题中清楚地说明了他的问题是什么.. 不知道你在哪里想出了他在他的代码中明确有的东西Key Word DataSource 不是支持一个空间显然解决了他的问题..

标签: c# asp.net database sql-server-2008 datasource


【解决方案1】:

在您的连接字符串中,它应该是“数据源”,而不是“数据源”。只需添加一个空格。

【讨论】:

  • 好的,这样就成功了。但是,我遇到了一个新错误。 “不存在从对象类型 System.Web.UI.WebControls.TextBox 到已知托管提供程序本机类型的映射。”
  • 当您从文本框中设置参数值时,您应该使用.Text 属性。像这样:cmd.Parameters.AddWithValue("Name", txtName.Text);
  • 哈,只是我在工作中的愚蠢行为!错过了这样的细节。感谢您的帮助!
猜你喜欢
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多