2014-11-04 56 views
0

即時通訊工作在一個存儲用戶信息的Web應用程序,我想從數據庫獲取userdetails到文本框中,我可以進行更改數據並使用按鈕將數據從相同的文本框存儲到數據庫的同一個表中。它就像更新/編輯用戶配置文件 到目前爲止,我能夠將數據獲取到文本框中,但我無法更新數據。頁面提交的數據,但有數據的基礎沒有改變如何從數據庫檢索數據到文本框,並將編輯的數據從同一個文本框保存到數據庫

我使用的HTML &代碼:

<div id="pagebody" style="width: 80%; height: 120%; margin: auto;"> 
    <div id="personalinfodiv" class="try" align="center" style="background-color: white; width: 80%; border: 1px solid gray; margin: auto"> 
     <h3 align="center" >Personal information</h3> 
     <table> 
      <tr><td><asp:Label ID="fname" runat="server" Text="First Name :" Font-Bold="true" ForeColor="GrayText"></asp:Label></td> <td><asp:TextBox cssClass="textbox1" ID="firstnametext" runat="server"></asp:TextBox></td></tr> 
      <tr><td><asp:Label ID="lname" runat="server" Text="Last Name :" Font-Bold="true" ForeColor="GrayText"></asp:Label></td> <td><asp:TextBox cssClass="textbox1" ID="lastnametext" runat="server"></asp:TextBox></td></tr> 
      <tr><td><asp:Label ID="gender" runat="server" Text="Gender :" Font-Bold="true" ForeColor="GrayText"></asp:Label></td> <td><asp:DropDownList cssClass="textbox1" ID="gendertext" runat="server" DataSourceID="SqlDataSource1" DataTextField="Gender" DataValueField="Gender"><asp:ListItem Text="Male" Value="1"></asp:ListItem><asp:ListItem Text="Female" Value="2"></asp:ListItem></asp:DropDownList> 
       <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Koshur %>" SelectCommand="SELECT [Gender] FROM [userdetails] WHERE ([Username] = @Username)"> 
        <SelectParameters> 
         <asp:QueryStringParameter Name="Username" QueryStringField="user" Type="String" /> 
        </SelectParameters> 
       </asp:SqlDataSource> 
       </td></tr> 
      <tr><td><asp:Label ID="dob" runat="server" Text="Date of Birth :" Font-Bold="true" ForeColor="GrayText"></asp:Label></td> <td><asp:TextBox cssClass="textbox1" ID="dobtext" runat="server"></asp:TextBox> 
       </td></tr> 
      <tr><td><asp:Label ID="Contactno" runat="server" Text="Contact No :" Font-Bold="true" ForeColor="GrayText"></asp:Label></td> <td><asp:TextBox cssClass="textbox1" ID="contacttext" runat="server"></asp:TextBox> 

       </td></tr> 
     </table> 

C#進行數據檢索:

string CSs = ConfigurationManager.ConnectionStrings["Koshur"].ConnectionString; 
using (SqlConnection conn = new SqlConnection(CSs)) 
{ 
    string query = "select * from userdetails where Username='" + HttpContext.Current.User.Identity.Name.ToString() + "';"; 
    SqlCommand cmd = new SqlCommand(query, conn); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    DataSet ds = new DataSet(); 
    da.Fill(ds, "userdetails"); 
    firstnametext.Text=ds.Tables["userdetails"].Rows[0]["Firstname"].ToString(); 
    lastnametext.Text = ds.Tables["userdetails"].Rows[0]["Lastname"].ToString(); 
    dobtext.Text = ds.Tables["userdetails"].Rows[0]["Dateofbirth"].ToString(); 
    contacttext.Text = ds.Tables["userdetails"].Rows[0]["ContactNO"].ToString(); 
} 

C#更新數據轉化爲表格並使用此存儲過程 即可

protected void savecontinue_Click(object sender, EventArgs e) 
{ 
    // Response.Redirect("test.aspx?q=" + firstnametext.Text +"&" + lastnametext.Text); 
    string ct = ConfigurationManager.ConnectionStrings["Koshur"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(ct)) 
    { 
     SqlCommand cmnd = new SqlCommand("spupdateuserprofiledetails", con); 
     cmnd.CommandType = CommandType.StoredProcedure; 

     SqlParameter first = new SqlParameter("@Firstname", firstnametext.Text); 
     SqlParameter last =new SqlParameter("@Lastname", lastnametext.Text); 

     SqlParameter dobb=new SqlParameter("@Dateofbirth", dobtext.Text); 
     SqlParameter connt=new SqlParameter("@ContactNo", contacttext.Text); 
     SqlParameter userna = new SqlParameter("@Username", HttpContext.Current.User.Identity.Name.ToString()); 

     cmnd.Parameters.Add(first); 
     cmnd.Parameters.Add(last); 
     cmnd.Parameters.Add(dobb); 
     cmnd.Parameters.Add(connt); 
     cmnd.Parameters.Add(userna); 

     con.Open(); 
     cmnd.ExecuteNonQuery(); 
    } 
} 
} 

這是我的存儲過程

create proc spupdateuserprofiledetails 
@Firstname varchar(100), 
@Lastname Varchar(100), 
@Dateofbirth varchar(100), 
@ContactNo varchar(100), 
@Username varchar(100) 
as 
begin 
update Userdetails 
set [email protected],[email protected],[email protected],[email protected] 
where [email protected] 

end 
+1

請問您還可以發佈您的spupdateuserprofiledetails? – fnupp 2014-11-04 13:17:32

+0

SELECT查詢中可能的SQL注入附加 – Ruskin 2014-11-04 13:27:14

+0

在SQL Server中,請勿對存儲的procs使用前綴「sp」:數據庫引擎首先在系統數據庫中查找那些首次將毫秒添加到處理時間的數據庫 – Ruskin 2014-11-04 13:28:00

回答

1

花了很多時間,但最終我能夠得到答案。這只是一個幸運的猜測。

我能夠通過包裝提高數據檢索的代碼來解決我的問題:

if (!IsPostBack) 
{ 
    string CSs = ConfigurationManager.ConnectionStrings["Koshur"].ConnectionString; 
    using (SqlConnection conn = new SqlConnection(CSs)) 
    { 
     string query = "select * from userdetails where Username='" + 
      HttpContext.Current.User.Identity.Name.ToString() + "';"; 
     SqlCommand cmd = new SqlCommand(query, conn); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     da.Fill(ds, "userdetails"); 
     firstnametext.Text = ds.Tables["userdetails"].Rows[0]["Firstname"].ToString(); 
     lastnametext.Text = ds.Tables["userdetails"].Rows[0]["Lastname"].ToString(); 
     dobtext.Text = ds.Tables["userdetails"].Rows[0]["Dateofbirth"].ToString(); 
     contacttext.Text = ds.Tables["userdetails"].Rows[0]["ContactNO"].ToString(); 
    } 
} 

這工作得很好。我能夠完美地檢索數據,並且能夠使用相同的文本框將其保存到數據庫中。這是我的基本要求。我沒有更改任何用於將數據保存到數據庫的代碼(即保存按鈕事件)。

+0

我刪除了帖子中的附加答案。如果您有新問題,請發佈新問題。如果這篇文章不是您的問題的真正解決方案,請不要將其作爲回答發佈,而是編輯您的原始問題。 – gunr2171 2014-12-24 15:50:24

+0

你應該在一個使用塊中「封裝」SqlCommand和SqlDataAdapter; [示例](http://stackoverflow.com/questions/15333522/sqldataadapter-with-using-keyword)。 – BCdotWEB 2014-12-24 16:00:04

+0

這是我的問題的答案,但我想知道發生了什麼(!IsPostBack)。我想要一個專家的解釋。我提到這是一個幸運的猜測。請回答。不要批評 – 2014-12-24 16:30:21

0

我認爲這是與你的SqlCommand集合添加參數的方式。或者可能發生傳遞給存儲過程的參數值(Username)與您在條件中指定的數據庫值不匹配的情況。可能會檢查@Username的值,並且UserName列的數據庫中的值不包含任何空格。

請嘗試下面的代碼,看看它是否有幫助。您可以使用AddWithValue進行類型的隱式轉換,並替換SqlParameterCollection.Add方法。這裏是link,你可以在這裏研究兩者之間的差異。

using (SqlConnection con = new SqlConnection(ct)) 
{ 
    using (SqlCommand cmnd = new SqlCommand("spupdateuserprofiledetails", con)) 
    { 
     cmnd.CommandType = CommandType.StoredProcedure; 

     string userName = HttpContext.Current.User.Identity.Name.ToString(); 
     cmnd.Parameters.AddWithValue("@FirstName", firstnametext.Text)); 
     cmnd.Parameters.AddWithValue("@LastName", lastnametext.Text)); 
     cmnd.Parameters.AddWithValue("@Dateofbirth", dobtext.Text)); 
     cmnd.Parameters.AddWithValue("@ContactNo", contacttext.Text)); 
     cmnd.Parameters.AddWithValue("@Username", userName)); 

     // Or If you are planning to use the `Add` method instead of the `AddWithValues` then make sure you explicitly specify the type of the parameter you pass to the stored procedure. 
     // cmnd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = firstnametext.Text; 
     // cmnd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = lastnametext.Text; 
     // and so on 

     con.Open(); 
     cmnd.ExecuteNonQuery(); 
    } 
} 
+0

當我嘗試執行SQL服務器中的SP它工作正常..數據更新。但通過網頁它劑量顯示任何變化,我也嘗試了ADDWITHVALUE,它也沒有工作。intitially我也認爲它不採取用戶名從「HttpContext.Current.User.Identity.Name.ToString());」我試圖找到另一種方法來查找文本框中的數據是真正更新還是沒有爲我從文本框發佈到其querystring中的另一個頁面,也有數據沒有改變..我已經寫了querystring代碼以及//部分。 。 – 2014-11-05 11:20:12

+1

請不要使用[AddWithValue](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/)。 – BCdotWEB 2014-12-24 15:57:31

相關問題