空字段不保存在数据库中 [英] Empty field not save in database

查看:85
本文介绍了空字段不保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我填写文本字段中的所有值时,程序正常工作正常。但问题是当txtMobileNo字段为空时我单击保存按钮时会出现错误将数据类型varchar转换为数字时出错。

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

尝试
{
string query = < span class =code-string> 插入CustomerTable值(' + txtCustomerName.Text + ',' + txtAddress.Text + ','
+ txtMobileNo.Text + ',' + lblGoldBalance.Text + ',' + lblSilverBalance.Text + ',' + lblCashBalance.Text + < span class =code-string>');

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(< span class =code-string> 用户已存在!!);
}
其他
{
如果(读者) != 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.Show( 只有移动字段可以为空);
}
}
catch (Exception ex)
{
MessageBox.Show( 至少0值可以保存在Gold Silver Cash Area,ex.Message)中;
}
最后
{
if (读者) != null
{
reader.Close();
}

如果(conn!= null
{
conn.Close();
}
}

解决方案

这只是猜测。我认为你在表格中是数字类型。当你没有通过它时,尝试插入'',这不是数字的有效类型。所以在你的情况下检查文本框是否为空,如果是这样通过0.但正确的解决方案是将字段更改为varchar并使其可为空。当文本框为空时传递NULL。


我认为最好的方法是使用参数化查询和可以为空的值。



参数化查询示例

Below Program is working fine when i am fill all the values in the textfield. but the problem is when txtMobileNo field empty and i click on the save button it gives a error"Error converting data type varchar to numeric".

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();
                }
            }

解决方案

This just a guess. I think you are datatype as numeric in table. When you don't pass it tries it to insert '' which is not a valid type for numeric. So in you case check if the text box is empty if so pass 0. But the right solution is to change the field to varchar and make it nullable. When the textbox is empty then pass NULL.


I believe that the best way to do that is to use Parameterized Query and with nullable values.

Parameterized Query Sample Here


这篇关于空字段不保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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