从数据库NOT WORKING中获取值后,通过文本框更新值

时间:2015-12-04 07:50:23

标签: c# asp.net sql-server asp.net-mvc-4 asp.net-web-api

我们从数据库中获取值并在加载页面时显示文本框然后在更新时,该页面的单击事件不起作用并自动重定向到母版页。我们需要在数据库中更新那些获取的值。

public partial class pages_update : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["id"].ToString());
        if (!Page.IsPostBack)
        {

            // txtname.Text = this.txtname.Text;
            SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=studreg;Integrated Security=True");
            DataTable dt = new DataTable();
            connection.Open();
            SqlDataReader iReader = null;

            /*
            if (id != 0)
            {
                if (!this.IsPostBack)
                {
                txtname.Text = id;
                //msg_lbl.Text = "Inside not PostBack";
            }
        }
        else
            Response.Write("Invalid URL for article");
        */


        string images = Request.QueryString["image1"];
        SqlCommand cmd = new SqlCommand("select id,(cast(fname as varchar(10))+''+cast(mname as varchar(10))+''+cast(lname as varchar(10)))as concatenated ,qualification,collage,dob,image,address,contact,email, technology from studreg", connection);
        iReader = cmd.ExecuteReader();
        int status = 0;
        while (iReader.Read())
        {
            int chkid = int.Parse(iReader["id"].ToString()); ;

            if (id == chkid && status == 0)
            {
                status = 1;
                // txtid.Text = (iReader["id"].ToString());
                txtname.Text = (iReader["concatenated"].ToString());
                txtqualification.Text = (iReader["qualification"].ToString());
                txtcollage.Text = (iReader["collage"].ToString());
                //  txtgender.Text = (iReader["gender"].ToString());
                txtdob.Text = (iReader["dob"].ToString());
                string path = string.Concat(@"/internship/pages/images/", iReader["image"].ToString());
                Image1.ImageUrl = path;
                txtaddress.Text = (iReader["address"].ToString());
                txtcontact.Text = (iReader["contact"].ToString());
                txtemail.Text = (iReader["email"].ToString());
                txttechnology.Text = (iReader["technology"].ToString());
             //   break;
            } //end if
        } // end while

    } // ennd ispostback

} // page load
//  connection.Close();




//protected void UPDATE_Click(object sender, EventArgs e)
protected void UPDATE_Click(object sender, EventArgs e)
    {

        SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=studreg;Integrated Security=True");
        SqlCommand cmd = new SqlCommand("update studreg set qualification=@qualification,collage=@collage,dob=@dob,address=@address,contact=@contact,email=@email,technology=@technology where id=@id", connection);

        if (connection.State != System.Data.ConnectionState.Open)
        {
            connection.Open();
            // cmd.Parameters.AddWithValue("@concatenated", txtname.Text);
            cmd.Parameters.AddWithValue("@qualification", txtqualification.Text);
            cmd.Parameters.AddWithValue("@collage", txtcollage.Text);
            cmd.Parameters.AddWithValue("@dob", txtdob.Text);
            cmd.Parameters.AddWithValue("@address", txtaddress.Text);
            cmd.Parameters.AddWithValue("@contact", txtcontact.Text);
            cmd.Parameters.AddWithValue("@email", txtemail.Text);
            cmd.Parameters.AddWithValue("@technology", txttechnology.Text);
            //   cmd.Parameters.AddWithValue("@id", txtid.Text);*/
            //SqlCommand cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            //cmd.ExecuteNonQuery(); 
            //  lblmsg.Text = "Successfully updated";
        }
        else
        {
            connection.Close();
        }
    }
}

按钮:

<asp:Button ID="UPDATE" class="btn btn-info pull-right" runat="server" Text="UPDATE" onClick="UPDATE_Click" ></asp:Button>

1 个答案:

答案 0 :(得分:0)

据我所知,您没有将public ActionResult AddressAndPayment(FormCollection values) 部分中定义的@id参数值添加为WHERE。看起来你需要取消注释该行;

where id = @id

如果不需要,请删除命令中的// cmd.Parameters.AddWithValue("@id", txtid.Text);*/ 参数。

还有一些事情;

Add

请注意,将所有参数添加为文本。根据它的名称,using(var connection = new SqlConnection(conStr)) using(var cmd = connection.CreateCommand()) { // Define your CommandText // Add your parameter values with Add method // Open your connection // Execute your query } 列应该是数字类型,而不是字符。