Rion Murph Murphy
Rion Murph Murphy

Reputation: 113

Blank values in SqlDatasource

After setting up my SqlDataSource on another page to display the values, they come up as 2 blanks for the 2 times I entered test values on the comments page.

I think I'm missing something in getting them into the table in the SQL Server database value?

I'm not sure what information is needed here, so please inform me.

Thanks in advance

EDIT #1 for user request for CODE

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

EDIT #2

The values seems to be empty for the Name, Phone, Email, and Comments in the database and when I test the query, so I think it's registering the entries, just not taking the values into the SQL?

EDIT #3

Due to a suggestion by coder and rs, I've done what they've said. And now I get this error. "String or binary data would be truncated. The statement has been terminated." The code has been updated as well.

EDIT #4

This question is a follow up for SQL Server Error, 'Keyword not supported 'datasource'.

Upvotes: 0

Views: 236

Answers (1)

coder
coder

Reputation: 13248

Remove all the "" similar to this txtPhone.Text = ""; before entering values to SQL as Server you're entering null values to that. So even if you give some values to the textbox it takes predefined NULL values and it dosen't enter either of them.

Upvotes: 1

Related Questions