AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Syed Adeel Rizvi
Expertise
I can answers questions regarding web based and desktop based programming in VB.Net. Which can include XML, Custom Controls + Computer Hardware and Windows Turboshooting...etc. More Skills : 1 . PHP - > All Type Of CMS e.g. (Joomla,Drupul,PHPNuke...etc) 2 . ASP 3 . ASP.Net 4 . CMS 5 . MySql 6 . SQl Server 2000 & 2005 7 . Ms Access 8 . Web Designing 9 . Networking Turboshooting 10 . Windows Turboshooting 11 . Hardware Turboshooting All Type Of Work Related To IT I also Do Work as a Freelance as Application & Web Developer & Designer

Experience
i have 3 years work experience in software house and 4 years work experience for computer hardware and Networking..

Organizations
110 Solutions

Education/Credentials
Bsc (Honors)

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > Sorry another query!

Topic: VB.NET



Expert: Syed Adeel Rizvi
Date: 4/11/2008
Subject: Sorry another query!

Question
Hi,

I have created a DataGrid and sucessfully managed to program it (manually) so that it displays records, I can then alter and update these records but I have yet to work out a sucessful way of deleting a record.

I can create a query in the access database that when it runs it deletes the row but I can not recreate this in the program.

Here is the code:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
<script runat="server">

   Dim Con As New System.Data.OleDb.OleDbConnection
   Dim ds As New Data.DataSet
   
   Dim da As Data.OleDb.OleDbDataAdapter
   Dim sqcon As Data.SqlClient.SqlConnection
   Dim sqcom As Data.SqlClient.SqlCommand
   Dim sqlSoftwareInstallation As String
   Dim sqlDelete As String
   
   
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

   End Sub
   
   Sub PopulateDataGrid()
       GetData()
       BindGV()
   End Sub
   Sub GetData()
       Con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Project\database\Backend.mdb"
       'Opens the connection to the database
       Con.Open()
       'This is table spercific
       sqlSoftwareInstallation = "SELECT * FROM tbl_SoftwareInstallation WHERE Tag = '" + Request("Tag") + "';"
       da = New Data.OleDb.OleDbDataAdapter(sqlSoftwareInstallation, Con)
       da.Fill(ds, "AmendInstallation")
       'Closes the connection to the database
       Con.Close()
   End Sub
   
   Private Sub BindGV()
       Try
           'Assigning Dataset to GridView
           GridView1.DataSource = ds
           'Bind Gridview
           GridView1.DataBind()
                       
       Catch ex As Exception
       End Try
   End Sub
   
      
   Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
       GridView1.EditIndex = e.NewEditIndex
       PopulateDataGrid()
   End Sub
   
   Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
       ' Get the new values from the GridView controls
       Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex
       Label1.Text = CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
      
       Dim InstallationID As String = CType(GridView1.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
       Dim Tag As String = CType(GridView1.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
       Dim SoftwareID As String = CType(GridView1.Rows(e.RowIndex).Cells(4).Controls(0), TextBox).Text
       Dim InstallationDate As String = CType(GridView1.Rows(e.RowIndex).Cells(5).Controls(0), TextBox).Text
              
       Con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Project\database\Backend.mdb"
       'Opens the connection to the database
       Con.Open()
       'This is table spercific
       sqlSoftwareInstallation = "SELECT * FROM tbl_SoftwareInstallation WHERE Tag = '" + Request("Tag") + "';"
       
       da = New Data.OleDb.OleDbDataAdapter(sqlSoftwareInstallation, Con)
       da.Fill(ds, "AmendInstallation2")
       
       Dim cb As New Data.OleDb.OleDbCommandBuilder(da)
       ds.Tables("AmendInstallation2").Rows(i).Item(0) = InstallationID
       ds.Tables("AmendInstallation2").Rows(i).Item(1) = Tag
       ds.Tables("AmendInstallation2").Rows(i).Item(2) = SoftwareID
       ds.Tables("AmendInstallation2").Rows(i).Item(3) = InstallationDate
       
       da.Update(ds, "AmendInstallation2")
       
       Con.Close()
       GridView1.EditIndex = -1
       PopulateDataGrid()
       
   End Sub
   Protected Sub GridView1_RowCancelling(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
       GridView1.EditIndex = -1
       PopulateDataGrid()
   End Sub

   Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
       PopulateDataGrid()
       Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex
       Dim InstallationID As String = (GridView1.Rows(i).Cells(2).Text)
       Label1.Text = InstallationID
       Con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Project\database\Backend.mdb"
       'Opens the connection to the database
       Con.Open()
       sqlDelete = "DELETE tbl_SoftwareInstallation.* FROM tbl_SoftwareInstallation WHERE InstallationID = " + InstallationID + ";"
       da = New Data.OleDb.OleDbDataAdapter(sqlDelete, Con)
       sqlSoftwareInstallation = "SELECT * FROM tbl_SoftwareInstallation WHERE Tag = '" + Request("Tag") + "';"
       da = New Data.OleDb.OleDbDataAdapter(sqlSoftwareInstallation, Con)
       da.Fill(ds, "AmendInstallation")
      
       
       Con.Close()
       GridView1.EditIndex = -1          
   End Sub
   
   Protected Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       PopulateDataGrid()
   End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
   Click to populate Datagrid:
   <asp:Button ID="btnStart" runat="server" OnClick="btnStart_Click" Text="Populate" />
   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
   <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
       <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
       <Columns>
           <asp:CommandField ShowEditButton="True" />
           <asp:CommandField ShowDeleteButton="True" />
       </Columns>
       <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
       <EditRowStyle BackColor="#999999" />
       <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
       <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
       <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
       <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

   </asp:GridView>

  
</asp:Content>

This is the part that im not sure how to do:
   Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
       PopulateDataGrid()
       Dim i As Integer = GridView1.Rows(e.RowIndex).DataItemIndex
       Dim InstallationID As String = (GridView1.Rows(i).Cells(2).Text)
       Label1.Text = InstallationID
       Con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Project\database\Backend.mdb"
       'Opens the connection to the database
       Con.Open()
       sqlDelete = "DELETE tbl_SoftwareInstallation.* FROM tbl_SoftwareInstallation WHERE InstallationID = " + InstallationID + ";"
       da = New Data.OleDb.OleDbDataAdapter(sqlDelete, Con)
       sqlSoftwareInstallation = "SELECT * FROM tbl_SoftwareInstallation WHERE Tag = '" + Request("Tag") + "';"
       da = New Data.OleDb.OleDbDataAdapter(sqlSoftwareInstallation, Con)
       da.Fill(ds, "AmendInstallation")
      
       
       Con.Close()
       GridView1.EditIndex = -1          
   End Sub

Any surgestions? All I want to do is delete the row based on that query.

Many Thanks

Ben

Answer
Hello,

this is not correct :        
sqlDelete = "DELETE tbl_SoftwareInstallation.* FROM tbl_SoftwareInstallation WHERE InstallationID = " + InstallationID + ";"

this is correct :

sqlDelete = "DELETE From tbl_SoftwareInstallation WHERE InstallationID = " & InstallationID & ";"

thank you

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.