我的代码显示错误,但我没有修复它? [英] There Shows An Error On My Code,But I Didn't Fix It?

查看:60
本文介绍了我的代码显示错误,但我没有修复它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
                con.Open();
                string userinfo = "select count(*) from [User] where UserName='" + TextBoxUN.Text + "'";
                SqlCommand com = new SqlCommand(userinfo, con);
                int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
                //int temp = (int)com.ExecuteScalar();
                if (temp > 0)
                {
                    Response.Write("User already exists");
                }
                con.Close();


            }
        }

       
        protected void Button1_Click1(object sender, EventArgs e)
        {
           try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
                con.Open();
                string insertQuiry = "INSERT INTO [User] ([UserName],E-mail,Password,sex,Birth,Country) values(@Uname,@email,@password,@sex,@birth,@country)";
                SqlCommand com = new SqlCommand(insertQuiry, con);
                com.Parameters.AddWithValue("@Uname", TextBoxUN.Text);
                com.Parameters.AddWithValue("@email", TextBoxmail.Text);
                com.Parameters.AddWithValue("@password", TextBoxP.Text);
                com.Parameters.AddWithValue("@sex", gend.SelectedItem.ToString());
                com.Parameters.AddWithValue("@birth",TextBoxbirth.Text);
                com.Parameters.AddWithValue("@country", DropDownListcountry.SelectedItem.ToString());
               com.ExecuteNonQuery();
              
                  Response.Redirect("manager.aspx");
                  Response.Write("Registration is succesful");

                  con.Close();
            }
            catch (Exception excep)
            {

                Response.Write("Error:" + excep.ToString());
            }
        }

推荐答案

错误告诉你究竟是什么问题,这是一个语法错误你在哪里有 - 。所以,只需在SQL语句中查看 - ,它只在一个地方,即电子邮件。



只需在字段名称周围使用方括号,[电子邮件],这样SQL就不会认为你试图从E中减去邮件,因此它知道[电子邮件]是一个字段名称。



并且,如果有机会,请更改数据库中的字段名称,使其没有短划线。



The error tells you exactly what the issue is, it's a syntax error where you have "-". So, just looking at where you have "-" in your SQL statement it is in only one place, E-mail.

Just use square brackets around the field name, [E-mail], so that SQL does not think you are trying to subtract mail from E and so that it understands that [E-mail] is a field name.

And, if you have the opportunity, change the field name in the db so it does not have the dash.

"INSERT INTO [User] ([UserName],[E-mail],Password,sex,Birth,Country) values(@Uname,@email,@password,@sex,@birth,@country)";






在[]中使用字段名称是一个好习惯。您还可以使用字符串前面的@符号来解决您所面临的问题,例如:



Hi,

Using field names in [] is a good habit. You can also resolve the issue that you are facing using "@" symbol before the string like:

string insertQuiry = @"INSERT INTO [User] ([UserName],E-mail,Password,sex,Birth,Country) values(@Uname,@email,@password,@sex,@birth,@country)";





我建议你使用上面的RyanDev解决方案。



谢谢&问候

Sisir Patro



I would recomend you to use "RyanDev's" solution above.

Thanks & Regards
Sisir Patro


这篇关于我的代码显示错误,但我没有修复它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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