Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a datagrid and a few text boxes with a submit button.the user enters the data and presses the submit button.The submit function is called and the data is entered in the database and the change is reflected in the datagrid.Now on refreshing the page,it again calls the submit button and enters the data into the databbase and thereby reflecting the changes in the datagrid.

here's the partial code:

 

sub Page_Load(Sender as object, e as EventArgs)

if not Page.IsPostBack then

FillDataGrid()

end if

end sub

 

sub Submit(Sender as object, e as EventArgs)

dim i,j as Integer

dim params(7) as String

dim strText as string

dim blnGo as boolean=true

----------

------

---------

---------

---------

--------

---------

end sub

 

 

 

i want the data not to get entered in the database again on refreshing the page(obviously).How shud i do this?

Posted

here is the full scripting code

 

<%@ Import namespace="System.Data.OleDb" %>

<%@ Import namespace="System.Data" %>

<%@ Page Language="vb"%>

<HTML>

<script runat="server">

 

 

dim conn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/banking.mdb")

sub Page_Load(Sender as object, e as EventArgs)

if not Page.IsPostBack then

FillDataGrid()

end if

end sub

 

sub Submit(Sender as object, e as EventArgs)

dim i,j as Integer

dim params(7) as String

dim strText as string

dim blnGo as boolean=true

 

j=0

 

for i=0 to AddPanel.Controls.Count -1

if AddPanel.controls(i).GetType is GetType(TextBox) then

strText=CType(AddPanel.Controls(i),TextBox).Text

if strText <> "" then

params(j)=strText

else

blnGo=false

lblMessage.Text=lblMessage.Text & "You forgot to enter a value for " & AddPanel.Controls(i).ID & "<p>"

lblMessage.Style("ForeColor")="Red"

end if

j=j+1

end if

next

 

if not blnGo then

exit sub

end if

 

dim strSQL as string="INSERT INTO tblUsers(FirstName,LastName,Address,City,State,Zip,Phone) values('" & params(0) & "','" & params(1) & "','" & params(2) & "','" & params(3) & "','" & params(4) & "','" & params(5) & "','" & params(6) & "')"

 

ExecuteStatement(strSQL)

 

 

FillDataGrid()

 

end sub

 

 

sub dgData_Edit(Sender as object, e as DataGridCommandEventArgs)

FillDataGrid(e.Item.ItemIndex)

end sub

 

 

sub dgData_Delete(Sender as object, e as DataGridCommandEventArgs)

 

----------

----------

end sub

 

sub dgData_Update(Sender as object, e as DataGridCommandEventArgs)

----------

--------

end sub

 

sub dgData_Cancel(Sender as object, e as DataGridCommandEventArgs)

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

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

end sub

 

sub dgData_PageIndexChanged(Sender as object, e as DataGridPageChangedEventArgs)

dgData.DataBind()

end sub

 

 

function UpdateDataStore( e as DataGridCommandEventArgs) as boolean

dim i,j as integer

dim params(7) as string

dim strText as string

dim blnGo as boolean= true

 

j=0

 

for i=1 to e.Item.Cells.Count - 3

strText=CType(e.Item.Cells(i).Controls(0),TextBox).Text

if strText <> "" then

params(j)=strText

j=j+1

else

blnGo=false

lblMessage.Text=lblMessage.Text & "You forgot to enter a value<p>"

end if

next

 

if not blnGo then

return false

exit function

end if

 

dim strSQL as string="Update tblUsers set FirstName='" & params(0) & "',LastName='"& params(1) & "',Address='"& params(2) & "',City='"& params(3) & "',State='"& params(4) & "',Zip='"& params(5) & "',Phone='"& params(6) & "'where ID = " & CType(e.Item.Cells(0).Controls(1),Label).Text

 

ExecuteStatement(strSQL)

return blnGo

end function

 

sub FillDataGrid(Optional EditIndex as integer=-1)

dim objCmd as new OleDbCommand("select * from tblUsers",Conn)

dim objReader as OleDbDataReader

 

 

 

try

objCmd.Connection.Open()

objReader=objCmd.ExecuteReader()

catch ex as Exception

lblMessage.Text = "Error retrieving from the database"

end try

 

dgData.DataSource=objReader

if not EditIndex.Equals(Nothing) then

dgData.EditItemIndex = EditIndex

end if

 

 

dgData.DataBind()

 

 

 

objCmd.Connection.Close()

end sub

 

 

function ExecuteStatement(strSQL)

 

dim objCmd as new OleDbCommand(strSQL,Conn)

 

try

objCmd.Connection.Open()

objCmd.ExecuteNonQuery()

catch ex as Exception

lblMessage.Text="Error Updating the database"

end try

 

objCmd.Connection.Close()

 

end function

 

</script>

 

 

 

 

and then the html coding is there.I guess this code is enough.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...