Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将数据库中的数据检索到文本框中,并将编辑后的数据从同一文本框保存到数据库中_C#_Asp.net_Database - Fatal编程技术网

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

C# 如何将数据库中的数据检索到文本框中,并将编辑后的数据从同一文本框保存到数据库中,c#,asp.net,database,C#,Asp.net,Database,我正在开发一个存储用户信息的web应用程序,我想将数据库中的userdetails放入文本框中,在文本框中我可以对数据进行更改,并使用按钮将相同文本框中的数据存储到数据库的相同表中。这就像更新/编辑用户配置文件一样 到目前为止,我能够将数据输入文本框,但无法更新数据。页面提交数据,但数据库中没有任何更改 我使用的html代码是: <div id="pagebody" style="width: 80%; height: 120%; margin: auto;"> <di

我正在开发一个存储用户信息的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将数据更新到表中,并 我正在为此使用存储过程

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 Firstname=@Firstname,Lastname=@Lastname,Dateofbirth=@Dateofbirth,ContactNO=@ContactNo 
where Username=@Username

end

我认为这与为SqlCommand集合添加参数的方式有关。或者,您传递给存储过程的参数值Username可能与您在条件中指定的数据库值不匹配。可能需要检查@Username的值以及数据库中Username列的值是否不包含任何空格

尝试下面的代码,看看是否有帮助。您可以使用AddWithValue进行类型的隐式转换,并替换SqlParameterCollection.Add方法。这里是你可以研究两者不同之处的地方

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();
   }
}

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

我可以通过包装数据检索代码来解决我的问题:

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();
    }
}

这很有效。我能够完美地检索数据,也能够使用相同的文本框将其保存到数据库中。这是我的基本要求。我没有更改将数据保存到数据库的任何代码,即“保存”按钮事件。

您是否也可以发布您的spupdateuserprofiledetails?可能的SQL注入附加在SQL Server的选择查询中,不要将前缀sp用于存储的进程:数据库引擎首先在系统数据库中查找这些进程,并将毫秒添加到处理时间中如果您拥有完整版本的SQL Server,请从工具中打开探查器,查看sp是否被调用存储进程工作正常,当我在SQL Server中执行它时,它会更新数据,但是在WebApplicationton中没有更新,当我尝试在sql server中执行sp时,我得到了以前的数据,它工作正常。。数据已更新。但是通过网页它没有显示任何变化,而且我也尝试了ADDWITHVALUE,它也不起作用。最初我还认为它不是从HttpContext.Current.User.Identity.Name.ToString获取用户名;我尝试了另一种方法来查找textbox中的数据是否真的在更新,因为我从textbox发布到其querystring中的另一个页面,数据也没有更改..我在//部分也编写了querystring代码..请不要建议使用。我从您的帖子中删除了其他答案。如果您有新问题,请发布新问题。如果此帖子不是问题的真正解决方案,请不要将其作为答案发布,而是编辑原始问题。您应该将SqlCommand和SqlDataAdapter封装在using block;中。这是我问题的答案,但我想知道发生了什么!我要一个专家的解释。我提到这是一个幸运的猜测。请回答。不要批评,注意一个多月前你没有得到这个答案的原因是你没有显示你的数据检索代码的上下文。如果您显示它在Page_Load内部,或者从Page_Load调用,您会立即收到关于If!我回来了,我知道。其实是我的错。我应该早点提的。。你能解释一下if的作用吗!是否将数据存储回数据库?这是令人困惑的,因为pageload事件仅用于数据检索
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();
    }
}