将数据类型nvarchar转换为bigint时出错

时间:2010-10-01 06:09:38

标签: asp.net sql ado.net asp-classic

这段代码给了我错误:

error in converting datatype nvarchar to bigint

这是我的表值:

   @customer_first_name nvarchar(50),
@customer_last_name nvarchar(50),
@customer_address nvarchar(max),
@gender nchar(10),
@date_of_birth datetime,
@customer_email_id varchar(50),
@customer_occuption varchar(50),
@customer_phone_number Bigint,
@nominee_name nchar(50),
@customer_id nvarchar(50),
@account_type nvarchar(50),
@account_number nvarchar(50),
@transfer_access_code nvarchar(50),
@account_balance Bigint,
@pin_code BigInt,
@nationality nvarchar(50),
@password nvarchar(50)
    
lblAmount.Text = HFamount .Value ;
int balamount = Convert.ToInt32(lblAmount.Text);
int Cpincode = Convert.ToInt32(txtpincode.Text);

long phone = Convert.ToInt64(txtphone.Text);
string cust_id = CUSTOMER_ID_GENERATOR();
string sqlquery = "INSERT INTO customer_details values(@customer_first_name,@customer_last_name,@customer_address,@gender,@date_of_birth,@customer_email_id,@customer_occuption,@customer_phone_number,@nominee_name,@customer_id,@account_type,@account_number,@transfer_access_code,@account_balance,@pin_code,@nationality,@password)";
string strconstring = ConfigurationManager.ConnectionStrings["ONLINE_BANKING2_ConnectionString"].ConnectionString;
SqlConnection mycon = new SqlConnection(strconstring);
mycon.Open();
SqlCommand  cmdRegister= new SqlCommand(sqlquery,mycon);
cmdRegister.Parameters.Add(new SqlParameter("@customer_first_name",txtcustname.Text.ToString()));
cmdRegister.Parameters.Add(new SqlParameter("@customer_last_name",txtcustlastname.Text.ToString()));
cmdRegister.Parameters.Add(new SqlParameter("@customer_address",txtAddress.Text.ToString()));
cmdRegister.Parameters.Add(new SqlParameter("@gender", rbtngender.SelectedItem.Value.ToString()));
cmdRegister.Parameters.Add(new SqlParameter("@date_of_birth", txtDOB.Text));
cmdRegister.Parameters.Add(new SqlParameter("@customer_email_id", txtemail.Text.ToString()));
cmdRegister.Parameters.Add(new SqlParameter("@customer_occuption",  txtcustoccupation.Text.ToString()));
cmdRegister.Parameters.Add(new SqlParameter("@customer_phone_number",  phone));
cmdRegister.Parameters.Add(new SqlParameter("@nominee_name",  txtnominee.Text.ToString()));
cmdRegister.Parameters.Add(new SqlParameter("@customer_id",  cust_id));
cmdRegister.Parameters.Add(new SqlParameter("@account_type",  accType.SelectedItem.Value.ToString()));
cmdRegister.Parameters.Add(new SqlParameter("@account_number",  ACCOUNT_NUMBER_GENERATOR()));
cmdRegister.Parameters.Add(new SqlParameter("@transfer_access_code",Transfer_code()));
cmdRegister.Parameters.Add(new SqlParameter("@account_balance",  balamount));
cmdRegister.Parameters.Add(new SqlParameter("@pin_code",  Cpincode));
cmdRegister.Parameters.Add(new SqlParameter("@nationality", DropCountry.SelectedItem.Value.ToString()));
cmdRegister.Parameters.Add(new SqlParameter("@password",txtpasswd2.Text.ToString()));
cmdRegister.ExecuteNonQuery();
mycon.Close();

请帮帮我

仅查看回复

1 个答案:

答案 0 :(得分:1)

有一件事是尝试使用int作为balamount变量的数据类型而不是long。然后请打印balamount变量的值并在您的问题中更新它。

问题可能与balamount中的任何非数字字符无关,因为在这种情况下,它将在Comvert.toInt32()本身的行中失败。

编辑:因为您使用的是“Insert into table values(...)"语法。您确定列顺序是否正确。为避免混淆,您可以将插入语法更改为”Insert in table (col1, col2, ... coln) values (@col1, @col2,... @coln)".这会让您更好地了解至于您要插入值的列。