“将数据类型varchar转换为数字时出错”。 [英] "Error converting data type varchar to numeric".

查看:432
本文介绍了“将数据类型varchar转换为数字时出错”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



private void btnSave_Click(object sender,EventArgs e)

{

string connstr = @Server = .\SQLEXPRESS; Initial Catalog = RPSJDB; Integrated Security = True; Max Pool Size = 100;

SqlDataReader reader = null;

SqlConnection conn = null;



尝试

{

string query =插入CustomerTable值('+ txtCustomerName.Text +','+ txtAddress.Text +','

+ txtMobileNo.Text +','+ lblGoldBalance.Text +','+ lblSilverBalance。文字+','+ lblCashBalance.Text +');

conn = new SqlConnection(connstr);



if (txtCustomerName.Text!=&txtAddress.Text!=)

{

conn.Open();

//检查CustomerTable中是否存在用户名



string query1 =从CustomerTable中选择txtCustomerName,其中txtCustomerName ='+ txtCustomerName.Text +';

SqlCommand cmd = new SqlCommand(query1,conn);

reader = cmd.ExecuteReader();



if(reader) != null && reader.HasRows)

{

//用户存在于db中做某事

MessageBox.Show(用户已经存在! !);

}



其他

{

if(读者) != null)

{

reader.Close(); //在建立新连接之前关闭阅读器

}



SqlCommand cmd1 = new SqlCommand(query,conn);

cmd1.ExecuteNonQuery();

txtCustomerName.Clear();

txtAddress.Clear();

txtMobileNo.Clear();

MessageBox.Show(值保存在数据库中);

}

reader.Close();

conn.Close();

}

else

{

MessageBox。显示(只有移动字段可以为空);

}

}

catch(exception ex)

{

MessageBox。显示(至少0值可以保存在金银现金区,ex.Message);

}

最后

{

if(读者!= null)

{

reader.Close();

}



if(conn!= null)

{

conn.Close();

}

}







当我没有显示错误在MobileNo Field中写入任何值。

如果将值放在所有文本框中,则完全保存该值。我希望它显示只有移动字段可以为空消息,当它为空时。告诉我我的代码中有什么问题。



转换数据类型varchar时出错到数字。

Dear All,

private void btnSave_Click(object sender, EventArgs e)
{
string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
SqlDataReader reader = null;
SqlConnection conn = null;

try
{
string query = "insert into CustomerTable values('" + txtCustomerName.Text + "','" + txtAddress.Text + "','"
+ txtMobileNo.Text + "','" + lblGoldBalance.Text + "','" + lblSilverBalance.Text + "','" + lblCashBalance.Text + "')";
conn = new SqlConnection(connstr);

if (txtCustomerName.Text != "" & txtAddress.Text != "")
{
conn.Open();
//Checking User Name Exists in CustomerTable

string query1 = "select txtCustomerName from CustomerTable where txtCustomerName='" + txtCustomerName.Text + "'";
SqlCommand cmd = new SqlCommand(query1,conn);
reader = cmd.ExecuteReader();

if (reader != null && reader.HasRows)
{
//User exists in db do something
MessageBox.Show("User Already Exists!!");
}

else
{
if (reader != null)
{
reader.Close(); // close the reader before making a new connection
}

SqlCommand cmd1 = new SqlCommand(query, conn);
cmd1.ExecuteNonQuery();
txtCustomerName.Clear();
txtAddress.Clear();
txtMobileNo.Clear();
MessageBox.Show("Values Save in DataBase");
}
reader.Close();
conn.Close();
}
else
{
MessageBox.Show("Only Mobile Field Can be Empty");
}
}
catch (Exception ex)
{
MessageBox.Show("At Least 0 value can be save in the Gold Silver Cash Area", ex.Message);
}
finally
{
if (reader != null)
{
reader.Close();
}

if (conn != null)
{
conn.Close();
}
}



it show an error when i m not write any value in MobileNo Field.
if put the value in all the textboxes then save the value perfectly. i want it show the "Only Mobile Field Can be Empty" message when it blank.so tell me what's the wrong in my code.

"Error converting data type varchar to numeric".

推荐答案

在插入数据库之前必须检查手机号码值(因为它是数据库中的数字数据类型)。

检查无论是否为null,都可以通过编写不同的查询来处理情况。
You must check mobile number value before inserting into database(because it's numeric datatype in your db).
Check whether its null or not, it is then handle situation by writing different queries.


这篇关于“将数据类型varchar转换为数字时出错”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆