将数据插入数据库时​​出错 [英] Error in inserting data into database

查看:87
本文介绍了将数据插入数据库时​​出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.Net和C#和SQLServer2005数据库创建一个网站,而我是
将数据插入数据库时​​,显示错误将数据类型varchar转换为数字时出错.有时,它显示将数据类型varchar转换为字符串时错误.
我没弄错地方.
我的代码是:

I am making a website using ASP.Net and C# with SQLServer2005 Database, while I am
inserting data into database,it showing the error Error converting data type varchar to numeric. Some time it shows Error converting data type varchar to string.
I am not getting where I am wrong.
My code is:

try
        {
            string ID = GenerateId("new_user", "user_id", 7, "CRN-");
            SqlCommand cmd=new SqlCommand("insert into new_user values('"+ ID +"','"+ txtname.Text.ToLower().Trim() +"','"+ txtcompany.Text.ToLower().Trim() +"','"+ txtdesig.Text.ToLower().Trim() +"','"+ txtcontact.Text.Trim() +"','"+ txtemail.Text.ToLower().Trim() +"','"+ txtadd.Text.ToLower().Trim() +"')",con );
            cmd.CommandType = CommandType.Text;
            con.Open();
            cmd.ExecuteNonQuery();
            string n1 = "";
            string c1 = "";
            string d1 = "";
            string p1 = "";
            string e1 = "";
            string a1 = "";
            string n2 = txtname.Text;
            string c2 = txtcompany.Text;
            string d2 = txtdesig.Text;
            string p2 = txtcontact.Text;
            string e2 = txtemail.Text;
            string a2 = txtadd.Text;
            SqlDataReader r2 = cmd.ExecuteReader();
            while (r2.Read())
            {
                n1 = r2.GetString(0);
                c1 = r2.GetString(1);
                d1 = r2.GetString(2);
                p1 = r2.GetString(3);
                e1 = r2.GetString(4);
                a1 = r2.GetString(5);
           if (n2 == n1 && c2 == c1 && d2 == d1 && p2 == p1 && e2 == e1 && a2 == a1)
                {
                    flag = true;
                    Response.Write("Record Already Exist");
                    break;
                }
            }
            
            con.Close();
}
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }



自动生成ID的地方,我为此做一个函数.我会在需要时提供它.
请帮助我.....



Where ID is auto generated and I make a function for it. I will provide it while required.
Please help me.....

推荐答案

生成的ID是字符串类型,如果是,则需要转换其类型,请使用convert函数将ID转换为varchar()并我希望它能起作用
ID generated is string type, if yes you need to convert its type use convert function to convert ID to varchar() and i hope it works




检查您的数据库表定义,还有一件事是在代码中放置断点,然后跟踪您在哪里得到该错误.

并在运行时检查您的值.如果有任何字符串类型值传递给声明为int的batatable列,则将引发该错误.


删除"以获取int类型值.


所有最好的
Hi,

check your database table definition And one more thing is place breakpoint in code then trace where u got that error.

And check your values in runtime If any string type values are passing to batatable column which is declared as int then that error''ll raise.


Remove '''' to int type values.


All the Best


请提供您的表结构或列数据类型,以便我们识别问题.

或.

请提供完整的错误详细信息.
Please provide your Table structure or columns datatype so we can identify the problem.

or.

please provide full detail of error.


这篇关于将数据插入数据库时​​出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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