C# SqlDatasource中的空白值

C# SqlDatasource中的空白值,c#,asp.net,database,sql-server-2008,datasource,C#,Asp.net,Database,Sql Server 2008,Datasource,在另一个页面上设置mySqlDataSource以显示值后,在注释页面上输入测试值的2次中,它们显示为2个空格 我想我在将它们放入SQL Server数据库值的表中时遗漏了什么 我不确定这里需要什么信息,所以请通知我 提前谢谢 为用户请求代码编辑#1 protected void btnSend_Click(object sender, EventArgs e) { Page.Validate("vld2"); SendMail(); lblMsgSend.Visible

在另一个页面上设置my
SqlDataSource
以显示值后,在注释页面上输入测试值的2次中,它们显示为2个空格

我想我在将它们放入SQL Server数据库值的表中时遗漏了什么

我不确定这里需要什么信息,所以请通知我

提前谢谢

为用户请求代码编辑#1

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

    //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("Data Source=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.Text);
            cmd.Parameters.AddWithValue("Phone", txtPhone.Text);
            cmd.Parameters.AddWithValue("Email", txtEmail.Text);
            cmd.Parameters.AddWithValue("Comments", txtComment.Text);

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

    txtPhone.Text = "";
    txtEmail.Text = "";
    txtName.Text = "";
    txtComment.Text = "";
}
编辑#2

数据库中的姓名、电话、电子邮件和注释的值似乎是空的,当我测试查询时,我认为它是在注册条目,而不是将值带入SQL

编辑#3

由于编码员和编码员的建议,我已经照他们说的做了。现在我得到了这个错误。 “字符串或二进制数据将被截断。语句已终止。” 代码也已更新

编辑#4


此问题是的后续问题。

删除所有类似于此
txtPhone.Text=“”
在将值输入到SQL as Server之前,您正在将空值输入到该服务器。因此,即使您给文本框指定了一些值,它也会接受预定义的空值,并且不会输入任何一个值。

您显示的是插入代码:那么-数据库中的值是否正确?意思:这是显示页面中的错误吗?还是在数据存储器中?另外:这4个控件是否有值?在这2次中,它们显示为2个空格的是什么?对于哪些字段,它们是空白的?您是否测试了连接字符串?您正在将代码中的
文本框
值设置为
(空字符串),然后读取该值。您希望它如何插入非空值?删除空字符串txtPhone.Text=“”;在您输入任何值之前。好吧,它起作用了,我只需进入我的SQL数据库,将电话号码长度更改为20,而不是11。非常感谢。