如何从数据库检索数据到文本框,并将编辑的数据从同一个文本框保存到数据库

问题描述:

即时通讯工作在一个存储用户信息的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

花了很多时间,但最终我能够得到答案。这只是一个幸运的猜测。

我能够通过包装提高数据检索的代码来解决我的问题:

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://*.com/questions/15333522/sqldataadapter-with-using-keyword)。 – BCdotWEB 2014-12-24 16:00:04

+0

这是我的问题的答案,但我想知道发生了什么(!IsPostBack)。我想要一个专家的解释。我提到这是一个幸运的猜测。请回答。不要批评 – 2014-12-24 16:30:21

我认为这是与你的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