SQL Server Error, 'Keyword not supported 'datasource'

早过忘川 提交于 2019-12-13 04:09:31

问题


I'm currently trying to INSERT some values into a datasource, then display them on another page using the DataList control. However, after some testing and experimentation, I've found that the error comes from the very beginning.

Here is the code I have bound to my button.

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;
        }
    }

Here is the image of my webpage and the error it displays.

Please let me know if you need additional information.

Thanks in advance.


回答1:


In your connection string, it should be "Data Source", not "DataSource". Just add a space.



来源:https://stackoverflow.com/questions/14928700/sql-server-error-keyword-not-supported-datasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!