p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ADO.NET (http://p2p.wrox.com/forumdisplay.php?f=109)
-   -   Update Statement some error (http://p2p.wrox.com/showthread.php?t=36923)

thas123 January 7th, 2006 05:37 PM

Update Statement some error
 
hi,

following is the code for editing.The problem is in the Onclick of edit button.The values are not getting updated.I tried updating the
textbox1's value.and i tried printing too.But its not working.
The output is the following.Say i am inputing value 20 in the
textbox1.But the value 20 doesnt get passed in the update statement.

UPDATE TEACHERS SET TNAME='' where TID ='5689'
'This is the UPDATE statement.
strSQL = "UPDATE TEACHERS SET TNAME='"& TextBox1.Text &"' where TID ='"& str &"' "

<html>
<script language="vb" runat="server">
Sub Page_Load
dim cv as string
dim username as string
dim email as string
dim sname as string
dim str as string

Dim strConnection As String = "user id=sa;password=admin;server=.;initial catalog=students"
Dim cn As SqlConnection = New SqlConnection(strConnection)
cn.Open()

str = (request.querystring("cv"))
Dim strSelect As String = _
        "SELECT * FROM teachers"
Dim cmd As SqlCommand = New SqlCommand(strSelect, cn)
Dim dr As SqlDataReader
dr = cmd.ExecuteReader()
while dr.Read
if str=dr("TID") then
TextBox1.Text = dr("TNAME")
TextBox2.text=dr("DESIGNATION")
TextBox3.text=dr("CONTACTNO")
TextBox4.text=dr("EMAIL")
TextBox5.text=dr("USERNAME")
TextBox6.text=dr("PWD")
end if
end while
cmd.dispose()
cn.close()
end sub

Sub edit(s As Object, e As EventArgs)
dim str as string
dim cv as string
dim p as string
Dim conClasf As SqlConnection
Dim dbstring As String
Dim strSQL As String
dim tid as string
str = (request.querystring("cv"))
lblRecsAff.Text = "hi"
response.write(lblRecsAff.text)
lblRecsAff.Text = ""
dbstring = "user id=sa;password=admin;server=.;initial catalog=students"
conClasf = New SqlConnection(dbstring)
conClasf.open()
strSQL = "UPDATE TEACHERS SET TNAME='"& TextBox1.Text &"' where TID ='"& str &"' "
response.Write(strsql)
Dim cmd As New SqlCommand(strSQL,conClasf)
cmd.ExecuteNonQuery()
cmd.Dispose()
conClasf.Close()
end sub
</script>
</HEAD>
<body>
<form action="edit.aspx" method="post" name="form1" runat="server" ID="Form2">
<DIV align=center>
<TABLE cellSpacing=0 cellPadding=0 width=800 border=0>
<TBODY>
<TR>
<TD width=768 height=416 vAlign=top><BR>
<TABLE cellSpacing=0 cellPadding=2 width=550 align=center border=0>
<TBODY>
<TR>
<TD class=OUTERTABLE vAlign=top>
<TABLE class=INNERTABLE-WHITE cellSpacing=1 cellPadding=1
width="98%" align=center border=0>

<TBODY>
<TR>
<TD width="1" height=11></TD>
<TD width="522"></TD>
<TD width="5"></TD>
</TR>
<TR>
<TD height=215></TD>
<TD vAlign=top>
<TABLE cellSpacing=1 cellPadding=1 width=525 border=0>

<TBODY>
<TR>
<TD class=HeadingTable width=517 colSpan=3>Edit
Teacher</TD></TR>
<TR>
<TD class=Heading2 width=517 colSpan=3>&nbsp;</TD></TR>
<TR>
<TD class=whitebg width=221>*
Teacher Name</TD>
<TD class=whitebg width=242>

  <p>
    <asp:TextBox ID="TextBox1" runat="server" />


  </p>
  </TD>
<TD class=whitebg width=42></TD></TR>
<TR>
<TD class=whitebg width=221>*
Designation</TD>
<TD class=whitebg width=242><asp:TextBox ID="TextBox2" runat="server" />
</TD>
<TD class=whitebg width=42></TD></TR>
<TR>
<TD class=whitebg width=221>*
Contact no's</TD>
<TD class=whitebg width=242>
  <asp:TextBox ID="TextBox3" runat="server" /></TD>
<TD class=whitebg width=42></TD></TR>
<TR>
<TD class=whitebg width=221>*
Email</TD>
<TD class=whitebg width=242>
  <asp:TextBox ID="TextBox4" runat="server" /></TD>
<TD class=whitebg width=42></TD></TR>
<TR>
<TD class=whitebg width=221>*
User Name</TD>
<TD class=whitebg width=242>
  <asp:TextBox ID="TextBox5" runat="server" /> </TD>
<TD class=whitebg width=42></TD></TR>
<TR>
<TD class=whitebg width=221>*
Password</TD>
<TD class=whitebg width=242>
  <asp:TextBox ID="TextBox6" runat="server" /></TD>
<TD class=whitebg width=42></TD></TR>
<TR>
<TD class=whitebg width=221></TD>
<TD class=whitebg width=242></TD>
<TD class=whitebg width=42></TD></TR>
<TR>
<TD class=whitebg width=221></TD>
<TD class=whitebg width=242>
   <asp:Button ID="Button1" Text="Edit" onclick="edit" runat="server"/>
   <asp:Button ID="Button2" text="Cancel" runat="server" />
</TD>
<TD class=whitebg width=42></TD>
</TR></TBODY>
</TABLE></TD>
<TD></TD>
</TR>
<TR>
<TD height=0></TD>
<TD></TD>
<TD></TD>
</TR>
</TBODY></TABLE></TD></TR>
<TR><asp:Label ID="lblRecsAff" runat="server" />
<TD class=OUTERTABLE height=3></TD></TR></TBODY></TABLE>
<P>&nbsp;</P> <BR> <BR></TD>
<TD width=32>&nbsp;</TD>
</TR>
<TR>
<TD height=172>&nbsp;</TD>
<TD>&nbsp;</TD>
</TR>
</TBODY></TABLE>
</DIV>
</form>
    </body>
</HTML>

planoie January 8th, 2006 10:30 AM

It look like the problem is that you are re-populating the textbox values on every page hit. Remember one of the fundamentals of ASP.NET: Every page hit at the server is a unique and independant page request. The server only knows the difference between each page request based on the post data. You need to check for IsPostback in your page load and only load the textboxes on the first page hit.

In page_load():

If Not IsPostback Then
   <do your textbox prefill from DB here>
End If

Now, when you click the update button, that prefill part won't run and you'll actually be updating with the values you manually enter.

-Peter


All times are GMT -4. The time now is 02:34 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.