【问题标题】:Update value through textbox after fetching them from database NOT WORKING从数据库中获取值后通过文本框更新值不起作用
【发布时间】:2016-03-09 02:10:57
【问题描述】:

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

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>

【问题讨论】:

  • 这与mvc或web-api无关。
  • 您没有在SqlCommand 中为id 提供值

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


【解决方案1】:

据我所知,您没有将在WHERE 部分上定义的@id 参数值添加为where id = @id。看来您需要取消注释该行;

// cmd.Parameters.AddWithValue("@id", txtid.Text);*/

如果不需要,或者删除命令中的@id参数。

还有一些事情;

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 
}

注意,您将所有参数添加为文本。根据它的名称,id 列应该是数字类型,而不是字符。

【讨论】:

  • 我们已经通过 url 传递了 id 并显示了与该 id 匹配的那个。只要匹配我们正在显示数据,请查看检查部分(id -> 通过 url 和 chkid-> 从数据库获取)。并且我们希望在单击更新按钮时根据 id 修改该数据。不需要修改id。
  • @JasithaUday 我不明白你的评论。你能澄清一下吗?
  • tinypic.com/r/24et0k1/9 请检查网址。我附上了截图,并解释了我想要做什么。在此先感谢:)
  • @JasithaUday 我无法打开图像,因为我现在正在工作。将您的所有信息放入您的问题中。
  • 我有一个表单,我根据 id 获取员工的详细信息并将其填充到表单内的文本框中。我们通过 url Ex - localhost:55920/internship/pages/update.aspx?id=12 传递 id。现在我希望通过更新按钮修改员工详细信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 2013-02-01
相关资源
最近更新 更多