0

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'.

9
  • you are showing insert code: so - are the values correct in the DB? meaning: is it an error in the display page? or in the data storage? also: did the 4 controls have values? Commented Feb 18, 2013 at 3:48
  • What is they come up as 2 blanks for the 2 times?For which fields are they blank? Commented Feb 18, 2013 at 3:49
  • Have you tested your connection string? Commented Feb 18, 2013 at 3:54
  • You are setting textbox values to "" (empty string) in your code and then reading that. How do you expect it to insert non empty values? Commented Feb 18, 2013 at 3:55
  • Remove the empty strings txtPhone.Text = ""; before you enter any values. Commented Feb 18, 2013 at 3:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, it worked, I just had to go into my SQL database and change the phone number length to 20 instead of 11. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.