469,890 Members | 2,340 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

Error while trying to access the sql server

I am trying to insert values into the table using the values from the form

I following is the code I am using
Expand|Select|Wrap|Line Numbers
  1. protected void Button_Click(object sender, EventArgs e)
  2. {
  3.     SqlConnection conn = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=MyFavs;Integrated Security=SSPI");
  5.     string insertstring = @"Insert into MyFavs values (@FirstName,@LastName,@Address,@City,@State,@Phone,@Email,@Password)";
  6.     SqlCommand cmd = new SqlCommand(insertstring, conn);       
  8.     cmd.Parameters.AddWithValue("@FirstName", SqlDbType.VarChar).Value = FirstNameTextBox.Text.ToString();
  9.     cmd.Parameters.AddWithValue("@LastName", SqlDbType.VarChar).Value = LastNameTextBox.Text.ToString();
  10.     cmd.Parameters.AddWithValue("@Address", SqlDbType.VarChar).Value = AddressTextBox.Text.ToString();
  11.     cmd.Parameters.AddWithValue("@City", SqlDbType.VarChar).Value = CityTextBox.Text.ToString();
  12.     cmd.Parameters.AddWithValue("@State", SqlDbType.VarChar).Value = StateTextBox.Text.ToString();
  13.     cmd.Parameters.AddWithValue("@Phone", SqlDbType.VarChar).Value = PhoneTextBox.Text.ToString();
  14.     cmd.Parameters.AddWithValue("@Email", SqlDbType.VarChar).Value = EmailTextBox.Text.ToString();
  15.     cmd.Parameters.AddWithValue("@Password", SqlDbType.VarChar).Value = PasswordTextBox.Text.ToString();
  17.     try
  18.     {
  19.         conn.Open();
  20.         cmd.ExecuteNonQuery();
  21.     }
  22.     catch (SqlException er)
  23.     {
  24.         Response.Write(er.StackTrace);
  25.     }
  26.     finally
  27.     {
  28.         conn.Close();
  29.     }
I get the following error when I execute the code

at System.Data.SqlClient.SqlConnection.OnError(SqlExc eption exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnErro r(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndW arning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteRead er(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderT ds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader( CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNo nQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

Any help would be appreciated.

Jun 11 '09 #1
1 1340
9,735 Expert Mod 8TB
I am unfamiliar with the AddWithValue method so I looked it up.
According to MSDN:
AddWithValue replaces the SqlParameterCollection.Add method that takes a String and an Object.
You are not passing it an Object, you are passing it Strings (text from the TextBox).

I recommend that you use the Add Method instead of the AddWithValue method since you are using strings.
Jun 16 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by Gianfranco | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.