i m working on a web application that stores user information, i want to get the userdetails from the database into textboxes where i can make changes to the data and use a button to store the data from the same text boxes to the same table of the database.it is like updating/ editing user profile so far i am able to get the data into textboxes but i am unable to update the data. the page submits the data but there is no change in the data base
the html & code i am using is:
<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# for data retrieval :
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# to update the data into table and i m using a stored procedure for this
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();
}
}
}
this is my stored procedure
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