title: Error:System.Data.SqlClient.SqlException (0x80131904): Error converting data type nvarchar to numeric
body: <p>I am trying to create a registration page, however encountered an additional error!
  What I'm trying to achieve is transfer the codes that are entered by the user into the database.
  However, I could not achieve the objective that I want to due to an error encountered after clicking on the submit button.
  Any help would be much appreciated!</p>
  
  <pre><code>Error Message:
  Error:System.Data.SqlClient.SqlException (0x80131904): Error converting data type nvarchar to     numeric. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task&amp; task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task&amp; task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Registration.submitButton_Click(Object sender, EventArgs e) in d:\Desktop\TemporarySter\Registration.aspx.cs:line 80 ClientConnectionId:c3fd51e0-9dc7-49ba-ab88-313267504802
  </code></pre>
  
  <p>My SQL Database codes</p>
  
  <pre><code>  CREATE TABLE [dbo].[Client] (
      [ClientNo]     INT            IDENTITY (1, 1) NOT NULL,
      [cFirstName]   NCHAR (10)     NOT NULL,
      [cLastName]    NCHAR (50)     NOT NULL,
      [cD.O.B]       DATE           NOT NULL,
      [cCompanyName] NVARCHAR (255) NOT NULL,
      [cAddress]     NVARCHAR (255) NOT NULL,
      [cCity]        NVARCHAR (255) NOT NULL,
      [cZipCode]     NCHAR (10)     NOT NULL,
      [cPhoneNo] NUMERIC (18)   NOT NULL,
      [cFax]         NUMERIC (18)   NOT NULL,
      [cEmail]       NVARCHAR (MAX) NOT NULL,
      [cUsername]    NVARCHAR (50)  NOT NULL,
      [cPassword]    NVARCHAR (50)  NOT NULL,
      CONSTRAINT [PK_Client] PRIMARY KEY CLUSTERED ([ClientNo] ASC)
  );
  </code></pre>
  
  <p>My aspx.cs codes for the registration page</p>
  
  <pre><code>  using System;
      using System.Configuration;
      using System.Data;
      using System.Linq;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.HtmlControls;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Xml.Linq;
  using System.Data.SqlClient;
  public partial class Registration : System.Web.UI.Page
  {
      static readonly string scriptErrorUsername =
          "&lt;script language =\"javascript\"&gt;\n" +
          "alert (\"Error - Username is already taken! Please key in another Username\");\n" +
          "&lt;/script&gt;";
      static readonly string scriptSuccessNewAccount =
          "&lt;script language=\"javascript\"&gt;\n" +
          "alert (\"Your account has been successfully created - Thank You!\");\n" +
          "&lt;/script&gt;";
      protected void Page_Load(object sender, EventArgs e)
      {
  
          if (IsPostBack)
          {
              SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Desktop\TemporarySter\App_Data\legitdatabase.mdf;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=true");
              conn.Open();
              string checkuser = "select count(*) from Client where cUserName= '" + userTB.Text + "'";
              SqlCommand com = new SqlCommand(checkuser, conn);
              int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
              if (temp == 1)
              {
                  Response.Write("Username is already taken! Please choose another username.");
              }
              conn.Close();
          }
      }
      protected void submitButton_Click(object sender, EventArgs e)
      {
          try
          {
              SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Desktop\TemporarySter\App_Data\legitdatabase.mdf;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=true");
              conn.Open();
              Type csType = this.GetType();
              SqlCommand com;
              SqlDataReader rdr;
              string strSQLSelect = "SELECT cUsername FROM Client ORDER BY cUsername";         
              com = new SqlCommand(strSQLSelect, conn);
              rdr = com.ExecuteReader();
  
              while (rdr.Read() == true)
              {
                  if (userTB.Text == (string)rdr["cUsername"])
                  {
                      ClientScript.RegisterStartupScript(csType, "Error", scriptErrorUsername);
                      conn.Close();
                      rdr.Close();
                      return;
                  }
              }
  
  
              string insertQuery = "insert into Client (cFirstName, cLastName, [cD.O.B], cCompanyName, cAddress, cCity, cZipCode, cPhoneNo, cFax, cEmail, cUsername, cPassword) values (@firstname,@lastname,@dob,@companyname,@address,@city,@zipcode,@phoneno,@fax,@email,@username,@password)";
              com = new SqlCommand(insertQuery, conn);
              com.Parameters.AddWithValue("@firstname", firstnameTB.Text);
              com.Parameters.AddWithValue("@lastname", lastnameTB.Text);
              com.Parameters.AddWithValue("@dob", dateTB.Text.ToString());
              com.Parameters.AddWithValue("@companyname", companyTB.Text);
              com.Parameters.AddWithValue("@address", addressTB.Text);
              com.Parameters.AddWithValue("@city", cityTB.Text);
              com.Parameters.AddWithValue("@zipcode", postalcodeTB.Text);
              com.Parameters.AddWithValue("@phoneno", contactnumberTB.Text.ToString());
              com.Parameters.AddWithValue("@fax", faxTB.Text.ToString());
              com.Parameters.AddWithValue("@email", emailTB.Text);
              com.Parameters.AddWithValue("@username", userTB.Text);
              com.Parameters.AddWithValue("@password", passTB.Text);
  
              com.ExecuteNonQuery();
              Response.Redirect("ClientLogin.aspx");
              Response.Write("Congratulations! Your registration is successful!");
              ClientScript.RegisterStartupScript(csType, "Success", scriptSuccessNewAccount);
              conn.Close();
  
  
          }
          catch (Exception ex)
          {
              Response.Write("Error:" + ex.ToString());
          }
  
  
  }
      protected void resetButton_Click(object sender, EventArgs e)
      {
  
  
          Response.Redirect("~/Registration.aspx", true);
  
      }
  
  } 
  </code></pre>