updating database problem in asp.net?

ApurvA

New member
JAI MATADI all . .

m working in ASP.NET 4.0 (VISUAL STUDIO ) with SQL SERVER 3.5
and m getting problem while updating my query in database in it ..

m fetching the username in my TEXTBOX1 and fetching his/her fullname in TEXTBOX2
suppose the username is a read only field but the user can change his/her full name ..(updating database here)

here is my code ..

-------------------------------------------
mytry.aspx
---------------------
<body>
<form id="form1" runat="server">
<div>
Username :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Full Name :
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Update" onclick="Button1_Click" />

</div>
</form>
</body>

------------------------------------------------------
mytry.aspx.cs
------------------------
using System.Data.SqlClient;
using System.Data;
using System.IO;
public partial class mytry : System.Web.UI.Page
{
public string connstr = @"my database connection string";

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string qry;
qry = "select * from signup where username='mittul'";
SqlCommand cmd = new SqlCommand(qry, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count != 0)
{
TextBox1.Text = dt.Rows[0][1].ToString();
TextBox2.Text = dt.Rows[0][4].ToString();

}
}




protected void Button1_Click(object sender, EventArgs e)
{
string a = TextBox2.Text;

SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string qry;
qry = "update signup set fullname='" + a + "' where username='" + TextBox1.Text + "'";

SqlCommand cmd = new SqlCommand(qry, conn);
int i;
i = cmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("Updated");
qry = "select * from signup where username='mittul'";
SqlCommand cmd1 = new SqlCommand(qry, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd1);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count != 0)
{
TextBox1.Text = dt.Rows[0][1].ToString();
TextBox2.Text = dt.Rows[0][4].ToString();

}
}
else
{
Response.Write("Error");
}

}
}

---------------------------------------------------------------------

but when m changing the fullname (TEXTBOX2) and clicking on my button m getting the "UPDATED" in response.write but m not able to update my database as m getting the same value in TEXTBOX2 which was stored previously only ..

tell me what should i do here as i have to fetch the fullname in TEXTBOX2 to let the user what he/she has written so far ..

tell me plz :(

thank you ..
 
Back
Top