2012-09-30 139 views
0

我有2頁,第1頁包括gridview,我做了一個linkbutton,將ID傳遞到另一個頁面(頁面2)。在第2頁上,我填寫10 textbox es,並且我有一個編輯信息按鈕。將值從一個頁面傳遞到另一個頁面並更新

此代碼是第1頁:

... 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:LinkButton ID="lbNextPage" runat="server" 
        PostBackUrl='<%# "~/secure/upst.aspx?id="+ Eval("ID_st") %>'>edit</asp:LinkButton> 
       &nbsp; 
      </ItemTemplate> 
     </asp:TemplateField> 

...

,這是第2頁代碼:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     txtID.Text = Request.QueryString["id"].ToString(); 
    } 
    SqlConnection con = new SqlConnection(strcon); 
    string query = "select * from user_st where ID_st = @id"; 
    SqlCommand cmd = new SqlCommand(query, con); 
     cmd.Parameters.AddWithValue("@id", stid); 
     con.Open(); 
     SqlDataReader dr = cmd.ExecuteReader(); 
     dr.Read(); 

     txtName.Text = dr["name"].ToString(); 
     txtFamily.Text = dr["family"].ToString(); 
     txtAddress.Text = dr["adres"].ToString(); 
     txtHomeTel.Text = dr["home_tel"].ToString(); 
     txtTahsilat.Text = dr["tahsilat"].ToString(); 
     txtTel.Text = dr["celphone"].ToString(); 
     txtEmail.Text = dr["email"].ToString(); 
     txtShoghl.Text = dr["shoghl"].ToString(); 
     txtAge.Text = dr["age"].ToString(); 
     txtFadername.Text = dr["fader_name"].ToString(); 
     txtIDnumber.Text = dr["melli_code"].ToString(); 
     txtShSh.Text = dr["sh_sh"].ToString(); 

} 

protected void btnOk_Click(object sender, EventArgs e) 
{ 


    Boolean res = false; 
    SqlConnection conn = new SqlConnection(strcon); 

    string famil = txtFamily.Text; 
    string name = txtName.Text; 
    string fader = txtFadername.Text; 
    string tahsil = txtTahsilat.Text; 
    Double telhome = Convert.ToDouble(txtHomeTel.Text); 
    string adres = txtAddress.Text; 
    Double cel = Convert.ToDouble(txtTel.Text); 
    string email = txtEmail.Text; 
    Double shsh = Convert.ToDouble(txtIDnumber.Text); 
    string shoghl = txtShoghl.Text; 
    int age = Convert.ToInt32(txtAge.Text); 
    Double melli = Convert.ToDouble(txtIDnumber.Text); 
    int id = Convert.ToInt32(txtID.Text); 


    string query = "update user_st set [email protected] ,[email protected] ,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected] ,[email protected],[email protected],[email protected],[email protected] where [email protected]"; 

    SqlCommand cmdup = new SqlCommand(query, conn); 
    cmdup.Parameters.AddWithValue("@name",name); 
    cmdup.Parameters.AddWithValue("@fader_name",fader); 
    cmdup.Parameters.AddWithValue("@family", famil); 
    cmdup.Parameters.AddWithValue("@tahsilat",tahsil); 
    cmdup.Parameters.AddWithValue("@adres", adres); 
    cmdup.Parameters.AddWithValue("home_tel",telhome); 
    cmdup.Parameters.AddWithValue("@celphone",cel); 
    cmdup.Parameters.AddWithValue("@email", email); 
    cmdup.Parameters.AddWithValue("@sh_sh", shsh); 
    cmdup.Parameters.AddWithValue("@shoghl", shoghl); 
    cmdup.Parameters.AddWithValue("@age",age); 
    cmdup.Parameters.AddWithValue("@melli_code", melli); 
    cmdup.Parameters.AddWithValue("@id", id); 


    try 
    { 
     conn.Open(); 
     cmdup.ExecuteNonQuery(); 
     conn.Close(); 
     res = true; 
    } 
    catch (SqlException ex) 
    { 
     lblRes.Text = "error" + ex.ToString(); 
    } 

    if (res) 
    { 
     lblResult.Text = "Ok"; 
    } 

那不是工作,所以,我想這:

 //cmdup.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = txtName.Text; 
    //cmdup.Parameters.Add("@fader_name", SqlDbType.NVarChar, 50).Value = txtFadername.Text; 
    //cmdup.Parameters.Add("@family", SqlDbType.NVarChar, 50).Value = txtFamily.Text; 
    //cmdup.Parameters.Add("@tahsilat", SqlDbType.NVarChar, 50).Value = txtTahsilat.Text; 
    //cmdup.Parameters.Add("@adres", SqlDbType.NVarChar, 150).Value = txtAddress.Text; 
    //cmdup.Parameters.Add("home_tel", SqlDbType.Char, 10).Value = txtHomeTel.Text; 
    //cmdup.Parameters.Add("@celphone", SqlDbType.Char, 10).Value = txtTel.Text; 
    //cmdup.Parameters.Add("@email", SqlDbType.VarChar).Value = txtEmail.Text; 
    //cmdup.Parameters.Add("@sh_sh", SqlDbType.Char, 10).Value = txtShSh.Text; 
    //cmdup.Parameters.Add("@shoghl", SqlDbType.NVarChar, 50).Value = txtShoghl.Text; 
    //cmdup.Parameters.Add("@age", SqlDbType.Int).Value = txtAge.Text; 
    //cmdup.Parameters.Add("@melli_code", SqlDbType.Char, 10).Value = txtIDnumber.Text; 
    //cmdup.Parameters.Add("@id", SqlDbType.Int).Value = txtID.Text; 

or this:

 //SqlCommand cmdup = new SqlCommand("EXEC up_st'" + txtName.Text. + "' , '" + txtFamily.Text + "' , '" + txtTahsilat.Text +"' , '" + txtAddress.Text + "' , '" 
    //         + txtHomeTel.Text + "' , '" + txtTel.Text + "' , '" + txtEmail.Text + "' , '" + txtShoghl.Text + "' , '" 
    //         + txtAge.Text + "' , '" + txtFadername.Text + "' , '" + txtIDnumber.Text + "' , '" + txtShSh.Text + "' , '" 
    //         + txtID.Text + "'", conn); 

或本:

 /*"update user_st set name='" + txtName.Text + "',fader_name='" + txtFadername.Text + "',family='" + txtFamily.Text + "',tahsilat='" + txtTahsilat.Text + "',adres='" + txtAddress.Text + "',home_tel='" + txtHomeTel.Text + "',celphone='" 
     + txtTel.Text + "',email='" + txtEmail.Text + "',sh_sh='" +      txtShSh.Text + "',shoghl='" + txtShoghl.Text + "',age='" + txtAge.Text + "',melli_code='" + txtIDnumber.Text + "' where ID_st=" + txtID.Text*/ 

,但它也不起作用。

+1

可否請你提的異常錯誤消息? –

+0

任何錯誤消息,該代碼沒有err工作,但與數據庫中的任何更改 – Strawberry

+0

您需要通過您的代碼,我的意思是在調試模式 – codingbiz

回答

1

您需要將您的初始,因爲發生的事情是要回發的更新,但文本框前的Page_Load大火被更新SQL選擇進入!的IsPostBack塊。所以一切工作都在你的初始代碼中,你只是用最初的信息更新它。試試這個:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     txtID.Text = Request.QueryString["id"].ToString(); 
     SqlConnection con = new SqlConnection(strcon); 
     string query = "select * from user_st where ID_st = @id"; 
     SqlCommand cmd = new SqlCommand(query, con); 
     cmd.Parameters.AddWithValue("@id", stid); 
     con.Open(); 
     SqlDataReader dr = cmd.ExecuteReader(); 
     dr.Read(); 

     txtName.Text = dr["name"].ToString(); 
     txtFamily.Text = dr["family"].ToString(); 
     txtAddress.Text = dr["adres"].ToString(); 
     txtHomeTel.Text = dr["home_tel"].ToString(); 
     txtTahsilat.Text = dr["tahsilat"].ToString(); 
     txtTel.Text = dr["celphone"].ToString(); 
     txtEmail.Text = dr["email"].ToString(); 
     txtShoghl.Text = dr["shoghl"].ToString(); 
     txtAge.Text = dr["age"].ToString(); 
     txtFadername.Text = dr["fader_name"].ToString(); 
     txtIDnumber.Text = dr["melli_code"].ToString(); 
     txtShSh.Text = dr["sh_sh"].ToString(); 
    } 
} 

如果你真的想從數據庫中重新加載的東西,你可以彈出該查詢到的函數,在按鈕點擊功能按鈕,點擊更新後重新運行它(末) ,但實際上沒有理由,因爲文本框已經具有相同的信息。

相關問題