Monday, March 12, 2012

Problem with Saving data in SQL Server

Hi,

I have a problem. The thing is that when I enter the values in a text boxes like evendate.text = "02/11/07" and desc.text="hello how r u" then I click on Save Button and its save in the SQL DATABASE but after this if I click on Refresh Button every time from the internet Explorer its automatically again save the data in the database although I have empty the textboxes.

The code is below:

<script runat="server">

sub SaveEvent(sender as object, e as EventArgs)

dim con as new SQLConnection("server=london-home; Database=tony; uid=rashid2; pwd=test; ")

dim cmd as new SQLCommand("insert into events values('" & desc.text & "','" & DateTime.Parse(eventdate.text) & "')",con)

con.open()

cmd.executenonquery()

con.close()
clearform()
display()

end sub

sub display()

dim con as new SQLConnection("server=london-home; Database=tony; uid=rashid2; pwd=test; ")
dim cmd as new SQLCommand("select * from events",con)

con.open()

dim SDR as SQLDataReader
SDR = cmd.ExecuteReader()


if SDR.HasRows = true then
ViewEvents.DataSource = SDR
ViewEvents.DataBind()

end if

con.close()


end sub

sub clearform()
eventdate.text=""
desc.text=""

end sub

sub Reset(sender as object, e as EventArgs)
clearform()

end sub
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>


<style type="text/css">

#display_prod{float:left;}

#prod{margin-top:0;}

</style>

</head>
<body>

<table border="1" align="center" cellpadding="5" cellspacing="0">
<tr>
<td valign="top">

<form id="prod" runat="server" enctype="multipart/form-data">
<table id="display_prod" width="554" border="1" cellspacing="0" cellpadding="0" align="left" height="400">
<tr valign="top" height="20">
<td> </td>
<td>
<table border="1" align="center" cellpadding="10" cellspacing="0">
<tr>
<td>PRODUCTS DESCRIPTION <asp:Label runat="server" ID="lbl"></asp:Label></td>
</tr>
</table>
</td>
</tr>

<tr valign="top">
<td>
<table align="left" id="menu" width="150" border="1" cellpadding="0" cellspacing="0">
<tr align="center">

<td><a href="http://links.10026.com/?link=home.aspx">Main</a></td>
</tr>
<tr align="center">
<td><a href="http://links.10026.com/?link=Events.aspx">Change Password</a></td>
</tr>
<tr align="center">
<td><a href="http://links.10026.com/?link=events.aspx">Events</a></td>
</tr>
</table>
</td>

<td align="left">
<table width="390" border="1" cellpadding="0" cellspacing="0">
<tr>
<td width="100" >Enter Date :</td>
<td width="243" ><asp:TextBox runat="server" ID="eventdate" BackColor="#FFFFFF" Width="100"></asp:TextBox> mm/dd/yy</td>
</tr>
<tr>
<td>Description :</td>
<td><asp:TextBox runat="server" ID="desc" TextMode="MultiLine" Columns="20" Rows="3" BackColor="#FFFFFF" MaxLength="200"></asp:TextBox> max 200</td>
</tr>
<tr>
<td> </td>
<td>
<asp:Button runat="server" ID="btnsubmit" Text="Save" OnClick="SaveEvent"></asp:Button>
<asp:Button runat="server" ID="btnreset" Text="Reset"></asp:Button>
</td>
</tr>
</table>

<table width="390" border="1" align="left" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:DataGrid runat="server" ID="ViewEvents" AutoGenerateColumns="false">
<columns>
<asp:TemplateColumn>
<itemtemplate>
<table id="tt" border="1" cellpadding="5" cellspacing="0">
<tr>
<td><asp:Label runat="server" ID="eventname" Text='<%#container.DataItem("event_name")%>' ></asp:Label></td>

<td><asp:Label runat="server" ID="pdesc" Text='<%# trim(DataBinder.Eval(Container.DataItem,"event_date","{0:MM/dd/yyyy}")) %>' ></asp:Label></td>
</tr>
</table>
</itemtemplate>
</asp:TemplateColumn>
</columns>
</asp:DataGrid>
</td>
</tr>
</table>
</td>
</tr>
</table>

</form>


</td>
</tr>
</table>
</body>
</html>

it will happend.. on think you can do after successfully save the data you can redirect the page with same URL.

|||

Thanks its solved now!

Here is a structure of my table

create table events
(
event_id integer primary key identity(1,1),
event_name varchar(200),
event_date datetime,
)

Note: The event_id is a automacally field and we dont need to insert record in this.

for example

i have inserted 10 records in a table

then i deleted all records manually from sql server

but when again I save the record the event_id field shows started from 11 even the table was empty, it should be start from 1?

Thanks for the co-operation

No comments:

Post a Comment