Button click event issue - asp.net

<section class="reg" id="reg">
<div class="row">
<div class="col-md-20">
<h3>Registration</h3>
<div class="box-area">
<div class="box-area-icon">
<div class="heading">
<h2>Registration</h2>
<h2><i class="fa fa-users"></i></h2>
</div>
</div>
<table style="width: 100%;" align="center">
<tr>
<td>Name</td>
<td class="text-left">
<asp:TextBox ID="vreg_name_txt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Contacct No.</td>
<td class="text-left">
<asp:TextBox ID="vreg_con_txt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Email</td>
<td class="text-left">
<asp:TextBox ID="vreg_email_txt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>State</td>
<td class="text-left">
<asp:DropDownList ID="vreg_state_dl" runat="server">
<asp:ListItem>gujrat</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>City</td>
<td class="text-left">
<asp:DropDownList ID="vreg_city_dl" runat="server">
<asp:ListItem>surat</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>Location</td>
<td class="text-left">
<asp:DropDownList ID="vreg_loc_dl" runat="server">
<asp:ListItem>katargam</asp:ListItem>
<asp:ListItem>adajan</asp:ListItem>
<asp:ListItem>piplod</asp:ListItem>
<asp:ListItem>chok</asp:ListItem>
</asp:DropDownList>
<br />
<asp:TextBox ID="vreg_area_txt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Username</td>
<td class="text-left">
<asp:TextBox ID="v_user_txt" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Password</td>
<td class="text-left">
<asp:TextBox ID="v_pass_txt" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">Confirm password</td>
<td class="auto-style3">
<asp:TextBox ID="v_conf_pass_txt" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="reg_btn" runat="server" Text="register" />
</td>
</tr>
</table>
</div>
</div>
</div>
</section>
I am writing a program to insert value in database on button click event. That program is using VB.NET. If I run the application, when I click the button, it does not fire a click event of the button. Help me to correct. Here's my code:
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient
Partial Class main
Inherits System.Web.UI.Page
Dim command As SqlCommand
Dim da As SqlDataAdapter
Dim ds As New DataSet
Dim i As Integer
Dim str As String = ConfigurationManager.ConnectionStrings("con").ConnectionString
Dim con As New SqlConnection(str)
'Protected Sub REGISTER_Click(sender As Object, e As EventArgs) Handles REGISTER.Click
'End Sub
Protected Sub Login_Click(sender As Object, e As EventArgs) Handles Login.Click
If (Page.IsValid) Then
Dim intusercount As SqlDataReader
Dim a, b As String
Using connection As New SqlConnection("Data Source=DESKTOP-LHSLB8R;Initial Catalog=temp_db;Integrated Security=True")
Using command As New SqlCommand("SELECT [v_id],[v_name] FROM [vendor_mstr] WHERE [v_user]=#Username AND [v_pass]=#Password", connection)
command.Parameters.Add("#Username", Data.SqlDbType.VarChar, 50).Value = username_txt.Text
command.Parameters.Add("#Password", Data.SqlDbType.VarChar, 50).Value = password_txt.Text
connection.Open()
intusercount = command.ExecuteReader
'Dim red As SqlDataReader =command.ExecuteReader
If intusercount.HasRows Then
While intusercount.Read()
MsgBox(intusercount.Item(0))
a = intusercount.Item(0)
b = intusercount.Item(1)
End While
Session("uid") = a
Session("user") = b
Response.Redirect("v_h.aspx")
Else
MsgBox("invalid username or password")
End If
connection.Close()
End Using
End Using
End If
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Session.Remove("uid")
Session.Remove("user")
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles reg_btn.Click
MsgBox("click")
Try
con.Open()
command = New SqlCommand("INSERT INTO vendor_mstr(v_name,v_area,v_contact,v_email,v_loc,v_user,v_pass,v_city,v_state)VALUES('" & vreg_name_txt.Text & "','" & vreg_area_txt.Text & "','" & vreg_con_txt.Text & "','" & vreg_email_txt.Text & "','" & vreg_loc_dl.SelectedValue & "','" & v_user_txt.Text & "','" & v_pass_txt.Text & "','" & vreg_city_dl.SelectedValue & "','" & vreg_state_dl.SelectedValue & "')")
command.Connection = con
command.ExecuteNonQuery()
con.Close()
MsgBox("registered :)")
Catch ex As Exception
MsgBox("not registered :(")
End Try
End Sub
End Class

change This
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles reg_btn.Click
with this :
Protected Sub reg_btn_Click(sender As Object, e As EventArgs) Handles reg_btn.Click

Related

When I selecting the combobox items its deleting or refreshing the attachments the file

I developed the compose message from email,
I attaching the files then i have selecting the combo box item its deleting the attachment files.
Here my ASP Code:
<asp:UpdatePanel ID="updatepanel" runat="server">
<ContentTemplate>
<tr>
<td valign="top">
<asp:Label ID="lblfile_name" runat="server" CssClass="labels">Upload File</asp:Label>
</td>
<td>
<div style="overflow-y: scroll; z-index: auto; height: 60px;">
<asp:FileUpload ID="FileUpload1" runat="server" CssClass="multi" Visible="true" />
</div>
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
<tr>
<td>
<telerik:RadComboBox EmptyMessage="----- Select -----" ID="cboTemplate" runat="server" Skin="WebBlue" AutoPostBack="true">
</telerik:RadComboBox>
</td>
</tr>
Here VB. NET CODE
Private Sub cboTemplate_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboTemplate.SelectedIndexChanged
If cboTemplate.SelectedValue = "--Select--" Then
lblErrMsg.Text = objcmnfunctions.GetErrMsg("B2B_WAR_110")
SetFocus(anc_err)
Exit Sub
End If
objdbconn.OpenConn()
msSQL = " Select mailtemplate_gid, template_name, template_content " & _
" from crm_trn_tmailtemplates " & _
" where mailtemplate_gid = '" & cboTemplate.SelectedValue & "'"
objOdbcDataReader = objdbconn.GetDataReader(msSQL)
If objOdbcDataReader.HasRows = True Then
objOdbcDataReader.Read()
radmailcontent.Content = objOdbcDataReader.Item("template_content").ToString
End If
objOdbcDataReader.Close()
objdbconn.CloseConn()
End Sub
Store the value of FileUpload1 to Session Object
If Session("FileUpload1") Is Nothing AndAlso FileUpload1.HasFile Then
Session("FileUpload1") = FileUpload1
ElseIf Session("FileUpload1") IsNot Nothing AndAlso (Not FileUpload1.HasFile) Then
FileUpload1 = DirectCast(Session("FileUpload1"), FileUpload)
ElseIf FileUpload1.HasFile Then
Session("FileUpload1") = FileUpload1
End If
The reason the dropdown is clearing the value from Upload is because it is set to AutoPostBack="true"

VB.NET SQL - String or binary data would be truncated. The statement has been terminated

I am new to this and getting the following error when running my form.
'String or binary data would be truncated.The statement has been terminated.'
This is the code for the form
<form runat="server">
<table style="width:500px">
<tr>
<td>Customer Name*: </td>
<td><asp:TextBox ID="CustomerName" runat="server" MaxLength="50"></asp:TextBox></td>
<td><asp:RequiredFieldValidator id="rfvValidator" runat="server" Enabled="true" ControlToValidate="CustomerName" ErrorMessage="Customer Name is Required." ></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>Address 1: </td>
<td><asp:TextBox ID="Address1" runat="server" MaxLength="100"></asp:TextBox></td>
</tr>
<tr>
<td>Address 2:</td>
<td><asp:TextBox ID="Address2" runat="server" MaxLength="100"></asp:TextBox></td>
</tr>
<tr>
<td>Address 3:</td>
<td><asp:TextBox ID="Address3" runat="server" MaxLength="100"></asp:TextBox></td>
</tr>
<tr>
<td>Town: </td>
<td><asp:TextBox ID="Town" runat="server" MaxLength="100"></asp:TextBox></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:TextBox ID="Country" runat="server" MaxLength="50"></asp:TextBox></td>
</tr>
<tr>
<td>Postcode:</td>
<td><asp:TextBox ID="PostCode" runat="server" MaxLength="8"></asp:TextBox></td>
</tr>
<tr>
<td>Telephone:</td>
<td><asp:TextBox ID="Telephone" runat="server" MaxLength="50"></asp:TextBox></td>
</tr>
<tr>
<td>Fax:</td>
<td><asp:TextBox ID="Fax" runat="server" MaxLength="50"></asp:TextBox></td>
</tr>
</table>
<asp:Button id="btnSave" Text="Save" runat="server" />
<input type=button name="cancel" value="Cancel" onClick="parent.jQuery.fancybox.close()">
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
This is my code for the submit button
Imports System.Data
Imports System.Data.SqlClient
Public Class EditCustomer
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Dim con As SqlConnection
Dim cmd As SqlCommand
Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
con = New SqlConnection("Data Source=IPAddress;Initial Catalog=medical01;Persist Security Info=True;User ID=******;Password=******")
con.Open()
cmd = New SqlCommand("INSERT INTO Customer_tbl (customername,address1,address2,address3,town,country,postcode,telephone,fax,isDeleted) VALUES('" & CustomerName.Text & "','" & Address1.Text & "','" & Address2.Text & "','" & Address3.Text & "','" & Town.Text & "', " & PostCode.Text & ", " & Telephone.Text & "," & Fax.Text & ", 1)", con)
cmd.ExecuteNonQuery()
Label1.Text = "Customer Added."
con.Close()
End Sub
End Class
Here is my database:
I am getting this error:
String or binary data would be truncated.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: String or binary data would be truncated.
The statement has been terminated.
You didn't quote all fields:
cmd = New SqlCommand("INSERT INTO Customer_tbl (customername,address1,address2,address3,town,country,postcode,telephone,fax,isDeleted)"
& "VALUES('" & CustomerName.Text & "','" & Address1.Text & "','" & Address2.Text & "','" & Address3.Text & "','" & Town.Text & "', '" & PostCode.Text & "', '" & Telephone.Text & "','" & Fax.Text & "', 1)", con)
I added them on PostCode, Telephone and Fax. This will help you out this time, but you should REALLY use parameters. It will make your life easier and this statement better:
cmd = New SqlCommand("INSERT INTO Customer_tbl (customername,address1,address2,address3,town,country,postcode,telephone,fax,isDeleted)"
& "VALUES(#customername,#address1,#address2,#address3,#town,#country,#postcode,#telephone,#fax,#isDeleted)", con)
cmd.Parameters.AddWithValue("customername", CustomerName.Text)
cmd.Parameters.AddWithValue("address1", Address1.Text)
cmd.Parameters.AddWithValue("address2", Address2.Text)
cmd.Parameters.AddWithValue("address3", Address3.Text)
cmd.Parameters.AddWithValue("town", Town.Text)
cmd.Parameters.AddWithValue("country", Country.Text)
cmd.Parameters.AddWithValue("postcode", PostCode.Text)
cmd.Parameters.AddWithValue("telephone", Telephone.Text)
cmd.Parameters.AddWithValue("fax", Fax.Text)
cmd.Parameters.AddWithValue("isDeleted", 1)

How to do Paging in a Datalist control?

I have my DataList in ASP.NET 3.5 Access 2003 DataSource. I want to create paging for a DataList control using VB.
DataList lacks functionality of pagging like in GridView but its possible to implement pagging functionality for DataList.
Here i have done pagging using PagedDataSource which enclose the paging related properties of a data-bound control, that allow it to perform the pagging. More details about PagedDataSource
Inline code
<div>
<asp:DataList ID="dataListStudent" runat="server">
<ItemTemplate>
<table cellpadding="10">
<tr>
<td nowrap="nowrap">
<b>Student id</b>
</td>
<td nowrap="nowrap">
<b>First name</b>
</td>
<td nowrap="nowrap">
<b>Last name</b>
</td>
<td>
<b>City</b>
</td>
</tr>
<hr />
<tr>
<td nowrap="nowrap">
<%# Eval("StudentID") %>
</td>
<td nowrap="nowrap">
<%# Eval("FirstName") %>
</td>
<td nowrap="nowrap">
<%# Eval("LastName") %>
</td>
<td nowrap="nowrap">
<%# Eval("City") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<br />
<table border="0" width="410">
<tr>
<td align="left">
<asp:LinkButton ID="lbPrev" runat="server">
Prev
</asp:LinkButton>
</td>
<td align="right">
<asp:LinkButton ID="lbNext" runat="server">
Next
</asp:LinkButton>
</td>
</tr>
</table>
</div>
Code-behind
Imports System.Data
Imports System.Data.OleDb
Partial Class _Default
Inherits System.Web.UI.Page
Dim pageds As New PagedDataSource()
Public Property CurrentPage() As Integer
Get
If Me.ViewState("CurrentPage") Is Nothing Then
Return 0
Else
Return Convert.ToInt16(Me.ViewState("CurrentPage").ToString())
End If
End Get
Set(ByVal value As Integer)
Me.ViewState("CurrentPage") = value
End Set
End Property
Sub bindDataList()
Dim sql As String
sql = "SELECT * FROM STUDENTS"
Dim da As New OleDbDataAdapter(sql, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Course.mdb")
Dim dt = New DataTable()
da.Fill(dt)
Try
pageds.DataSource = dt.DefaultView
pageds.AllowPaging = True
pageds.PageSize = 3
pageds.CurrentPageIndex = CurrentPage
lbNext.Enabled = Not pageds.IsLastPage
lbPrev.Enabled = Not pageds.IsFirstPage
dataListStudent.DataSource = pageds
dataListStudent.DataBind()
Catch ex As Exception
Throw ex
End Try
End Sub
Protected Sub lbPrev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbPrev.Click
currentPage -= 1
bindDataList()
End Sub
Protected Sub lbNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbNext.Click
currentPage += 1
bindDataList()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
bindDataList()
End If
End Sub
End Class
Your connection string, Column names might be different you might want different layout of DataList feel free to fiddle with it. Hopefully it will help you to solve the problem.

Listview with Search and DataPager controls

As this is my first project, I really do not have much idea to debug this. I have a listview. I am writing code manually to populate listview. As the listview is very large, I have a search button where user enters a search string. I am trying to build a sql string from here and populate the listview. Everything works fine but when I click on next page, it does not include the search string. I think it is just populating the 2nd page without the search criteria. If I remove the data pager control, it works fine. I am not able to put proper code datapger_PagePropertiesChanging. I appreciate if someone can help me with this.
Here is my list view markup:
<asp:ListView ID="lvContractors" runat="server" OnPagePropertiesChanging="lvContractors_PagePropertiesChanging">
<LayoutTemplate>
<div class="ListViewGrid">
<table class="tblpadding">
<tr>
<th class="HeaderStyle">
Contractor Number
</th>
<th class="HeaderStyle">
First Name
</th>
<th class="HeaderStyle">
Last Name
</th>
<th class="HeaderStyle">
View
</th>
<th class="HeaderStyle">
Edit
</th>
</tr>
<tbody><asp:PlaceHolder runat="server" ID="itemPlaceholder" /></tbody>
</table>
</div> <!-- End of ListViewGrid -->
</LayoutTemplate>
<ItemSeparatorTemplate> <td> <hr /> </td>
</ItemSeparatorTemplate>
<ItemTemplate>
<div >
<tr>
<td class="RowStyle"><%#Eval("ContractorNumber")%></td>
<td class="RowStyle"><%#Eval("FirstName")%></td>
<td class="RowStyle"><%#Eval("LastName")%></td>
<td class="RowStyle">
<asp:HyperLink ID="lnkView" runat="server" CssClass="link"
NavigateUrl='<%# "ContractorEdit.aspx?ID=" + Eval("ContractorNumber") + "&Mode=" + "CV" %>'
Target="_blank" Text="View" />
</td>
<td class="RowStyle">
<asp:HyperLink ID="lnkEdit" runat="server" CssClass="link"
NavigateUrl= '<%# "ContractorEdit.aspx?ID=" + Eval("ContractorNumber") + "&Mode=" + "CE" %>'
Text="Edit"> </asp:HyperLink>
</tr>
</div>
</ItemTemplate>
<AlternatingItemTemplate>
<div>
<tr>
<td class="AlternatingRowStyle"><%#Eval("ContractorNumber")%></td>
<td class="AlternatingRowStyle"><%#Eval("FirstName")%></td>
<td class="AlternatingRowStyle"><%#Eval("LastName")%></td>
<td class="AlternatingRowStyle">
<asp:HyperLink ID="lnkView" runat="server" CssClass="link"
NavigateUrl='<%# "Contractor.aspx?ID=" + Eval("ContractorNumber")%>'
Target="_blank" Text="View" />
</td>
<td class="AlternatingRowStyle">
<asp:HyperLink ID="lnkEdit" runat="server" CssClass="link"
NavigateUrl= '<%# "ContractorEdit.aspx?ID=" + Eval("ContractorNumber") + "&Mode=" + "CE" %>'
Text="Edit"> </asp:HyperLink>
</tr>
</div>
</AlternatingItemTemplate>
<ItemSeparatorTemplate>
<tr>
<td colspan="5" class="itemseparator"></td>
</tr>
</ItemSeparatorTemplate>
</asp:ListView>
<div class="pager">
<asp:DataPager PageSize="20" ID="DataPagerContractor" runat="server" PagedControlID="lvContractors"
NextPreviousButtonCssClass="PrevNext"
CurrentPageLabelCssClass="CurrentPage"
NumericButtonCssClass="PageNumber">
<fields>
<asp:NumericPagerField
PreviousPageText="< Prev"
NextPageText="Next >"
ButtonCount="10"
NextPreviousButtonCssClass="PrevNext"
CurrentPageLabelCssClass="CurrentPage"
NumericButtonCssClass="PageNumber" />
</fields>
</asp:DataPager>
</div>
</div> Code behind this: Partial Class Contractor
Inherits System.Web.UI.Page
Private txtKeywordSearchWhereClause As String = ""
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Session("Clicked") = "Contractor"
End If
If Not DBNull.Value.Equals(txtKeywordSearchWhereClause) Then
LoadContractors(txtKeywordSearchWhereClause)
Else
LoadContractors("")
End If
End Sub
Private Sub LoadContractors(ByVal strSearch As String)
Dim strConn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString)
strConn.Open()
Dim sql As String = "SELECT * FROM tblContractor1"
If Not strSearch = "" Then
sql = sql & strSearch
sql = sql & " ORDER BY LastName"
End If
Dim da As SqlDataAdapter = New SqlDataAdapter(sql, strConn)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
lvContractors.DataSource = dt
lvContractors.DataBind()
End Sub
Protected Sub buttonAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonAdd.Click
Response.Redirect("ContractorEdit.aspx", False)
End Sub
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
' Keyword Search
Dim txtKeywordSearch As String = txtSearch.Text
Dim txtWhereClause As String = ""
If txtKeywordSearch.Trim() <> "" Then
txtWhereClause = " WHERE FirstName LIKE '%" + txtKeywordSearch + "%'"
txtWhereClause = txtWhereClause + " OR LastName LIKE '%" + txtKeywordSearch + "%'"
txtWhereClause = txtWhereClause + " OR ContractorNumber LIKE '%" + txtKeywordSearch + "%'"
txtKeywordSearchWhereClause = txtWhereClause
End If
LoadContractors(txtKeywordSearchWhereClause)
End Sub
Protected Sub lvContractors_PagePropertiesChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs) Handles lvContractors.PagePropertiesChanging
DataPagerContractor.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
'rebind List View
If Not DBNull.Value.Equals(txtKeywordSearchWhereClause) Then
LoadContractors(txtKeywordSearchWhereClause)
Else
LoadContractors("")
End If
End Sub
End Class
Sorry to say, but this is not possible with the datapager (at least in ASP.Net 3.5).
The only solution is to write your own pager from scratch as the only querystring parameter the datapager can pass is the page number.

Page_Load not update after click the button

I have a simple ASP.NET page:
sub Page_Load
//Get data form databse and show it
end sub
sud deletsome(Source As Object, e As EventArgs)
//delete one record when user click on submit button
end sub
When I click the button, the page reload, all the data have no change, I must re-enter the page again, the record I have delete disappear.
Can you show me why?
The full code here:
<%# Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;data source=" & server.mappath("/data/test.accdb"))
dbconn.Open()
sql="SELECT * FROM [user]"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
sub deletesome(Source As Object, e As EventArgs)
dim dbconn,sql,dbcomm
dbconn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;data source=" & server.mappath("/data/test.accdb"))
dbconn.Open()
sql="DELETE FROM [user]WHERE id = #ID"
dbcomm=New OleDbCommand(sql,dbconn)
dbcomm.Parameters.AddWithValue("ID", tb1.Text)
dbcomm.ExecuteNonQuery()
end sub
</script>
<html>
<body>
<form runat="server">
<asp:TextBox id="tb1" runat="server" />
<asp:Button id="b1" Text="Submit" runat="server" OnClick="deletesome" />
<asp:Repeater id="customers" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr bgcolor="#b0c4de">
<th>ID</th>
<th>Address</th>
<th>City</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="#f0f0f0">
<td><%#Container.DataItem("id")%> </td>
<td><%#Container.DataItem("username")%> </td>
<td><%#Container.DataItem("userphone")%> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
<hr />
</body>
</html>
In asp page life cycle, page load happens before the button click (I know, it's kind of strange). The easiest work around is to place your code in the page "PreRender" event.
Use if(!IsPostBack) on page load.
You have not added the !IsPostBack condition in your page load event. When you click the button your page's load event is first called before the click event handler and it will reload the data again. That's the issue.
It should be like...
sub Page_Load
IF(!IsPostBack) ' This condition will be true when the page loads the first time
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;data source=" & server.mappath("/data/test.accdb"))
dbconn.Open()
sql="SELECT * FROM [user]"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
EndIf
end sub
try this
<%# Import Namespace="System.Data.OleDb" %>
<script runat="server">
sub Page_Load
If Page.IsPostBack = False Then
LoadData()
end sub
sub deletesome(Source As Object, e As EventArgs)
dim dbconn,sql,dbcomm
dbconn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;data source=" & server.mappath("/data/test.accdb"))
dbconn.Open()
sql="DELETE FROM [user]WHERE id = #ID"
dbcomm=New OleDbCommand(sql,dbconn)
dbcomm.Parameters.AddWithValue("ID", tb1.Text)
dbcomm.ExecuteNonQuery()
LoadData()
end sub
sub LoadData
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;data source=" & server.mappath("/data/test.accdb"))
dbconn.Open()
sql="SELECT * FROM [user]"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
</script>
<html>
<body>
<form runat="server">
<asp:TextBox id="tb1" runat="server" />
<asp:Button id="b1" Text="Submit" runat="server" OnClick="deletesome" />
<asp:Repeater id="customers" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr bgcolor="#b0c4de">
<th>ID</th>
<th>Address</th>
<th>City</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="#f0f0f0">
<td><%#Container.DataItem("id")%> </td>
<td><%#Container.DataItem("username")%> </td>
<td><%#Container.DataItem("userphone")%> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
<hr />
</body>
</html>

Resources