请帮助调试并恢复:值不进入数据库而不抛出错误。请帮助 [英] Pls help Debug and revert: Values not entering into the database and not throwing Error. pls help

查看:59
本文介绍了请帮助调试并恢复:值不进入数据库而不抛出错误。请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    private Boolean InsertUpdateData(SqlCommand cmd)
    {
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["lateefConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            return false;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {

   //   using(SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["lateefConnectionString"].ConnectionString))
        {
            try
            {
                string strQuery = "INSERT INTO [Register],[firstname],[lastname],[email],[Password],[Gender],[Dob],[Mobile],[department],[Address]) VALUES (@firstname,@lastname,@email,@Password,@Gender,@Dob,@Mobile,@department,@Address)";

                SqlCommand cmd = new SqlCommand(strQuery);

                //            const string SQL = "INSERT INTO Register values(@firstname,@lastname,@email,@Password,@Gender,@Dob,@Mobile,@department,@Address)";

                //adding parameters with value
                cmd.Parameters.AddWithValue("@firstname", txtFirstName.Text.ToString());
                cmd.Parameters.AddWithValue("@lastname", txtLastName.Text.ToString());
                cmd.Parameters.AddWithValue("@lastname", txtEmail.Text.ToString());
                cmd.Parameters.AddWithValue("@password", txtPassword.Text.ToString());
                cmd.Parameters.AddWithValue("@Gender", RdoGender.SelectedItem.Text.ToString());
                cmd.Parameters.AddWithValue("@Dob", txtDob.Text.ToString());
                cmd.Parameters.AddWithValue("@Mobile", txtMobile.Text.ToString());
                cmd.Parameters.AddWithValue("@department", TxtDpt.Text.ToString());
                cmd.Parameters.AddWithValue("@Address", txtAddress.Text.ToString());
                InsertUpdateData(cmd);

                lblMsg.Text= "Submission successful with Track ID";
            }
            catch
            {
            //  lblMsg.Text = "Not successful.";

            }
        }
    }
}

推荐答案

你是得到一个错误,但因为你的捕获是空的,你没有看到它。您的插入语句不正确。这样做..



You are getting an error but because your catch is empty you aren't seeing it. Your insert statement is not correct. Do this instead..

INSERT INTO tableName (field1, field2)
VALUES (@value1, @value2)


更改您的查询



string strQuery =INSERT INTO [Register],[firstname],[lastname],[email],[Password],[Gender],[Dob],[Mobile],[department],[地址])VALUES(@ firstname,@ lastname,@ email,@ Password,@ Gender,@ Dob,@ Mobile,@ department,@ Address);







string strQuery =INSERT INTO([注册],[名字],[姓氏],[电子邮件],[密码],[性别],[Dob] ],[移动],[部门],[地址])价值(@注册,@ firstname,@ lastname,@ email,@密码,@性别,@ Dob,@ Mobile,@ department,@地址);



你缺少(括号和@register参数。
Change your query from

string strQuery = "INSERT INTO [Register],[firstname],[lastname],[email],[Password],[Gender],[Dob],[Mobile],[department],[Address]) VALUES (@firstname,@lastname,@email,@Password,@Gender,@Dob,@Mobile,@department,@Address)";

To

string strQuery = "INSERT INTO ([Register],[firstname],[lastname],[email],[Password],[Gender],[Dob],[Mobile],[department],[Address]) VALUES (@register,@firstname,@lastname,@email,@Password,@Gender,@Dob,@Mobile,@department,@Address)";

you are missing "(" bracket and @register parameter.


这篇关于请帮助调试并恢复:值不进入数据库而不抛出错误。请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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