Building a Grdivew with Web Service Data - asp.net

Hi i am trying to build a Gridview Based on my Web Service Data.
I am not not getting any errors when I compile the code, however the gridview does not display. Not sure if this is because I am building the gridview wrong or the data is not being pulled correctly. The web service does work because I have tested it.
Here is the code i am using to bind the gridview:
Private Sub BindGrid()
Dim objVen As New ISISVendor.VendorInterfaceClient
Dim LoginResp As New ISISVendor.LoginResponse
Dim POResp As New ISISVendor.PoSummaryResponse
Dim pod As New ISISVendor.PoDetailResponse
Dim dt As New DataTable
dt.Columns.Add("Status")
dt.Columns.Add("Sender")
dt.Columns.Add("PO Number")
dt.Columns.Add("Date")
dt.Columns.Add("Action")
gvv.DataSource = dt
POResp = objVen.GetOpenPos("3274")
Dim dr As DataRow
If POResp.Pos.Count > 0 Then
For j As Integer = 0 To POResp.Pos.Count - 1
dr = dt.NewRow
dr.Item("Status") = POResp.Pos(j).Status
dr.Item("Sender") = "COMPANY TEST"
dr.Item("PO Number") = POResp.Pos(j).PoNumber
dr.Item("Date") = POResp.Pos(j).PoDate
dr.Item("Action") = ""
Next
End If
gvv.DataBind()
End Sub

Seem you are not adding the datarow to the data table
Please try as below
POResp = objVen.GetOpenPos("3274")
Dim dr As DataRow
If POResp.Pos.Count > 0 Then
For j As Integer = 0 To POResp.Pos.Count - 1
dr = dt.NewRow
dr.Item("Status") = POResp.Pos(j).Status
dr.Item("Sender") = "COMPANY TEST"
dr.Item("PO Number") = POResp.Pos(j).PoNumber
dr.Item("Date") = POResp.Pos(j).PoDate
dr.Item("Action") = ""
dt.Rows.Add(dr)
Next
End If
gvv.DataSource = dt
gvv.DataBind()

Related

How to iterate between two datatables in ASP.NET

I have two datatables (dtSF and CurveFitTable) that contain two different values which I have to multiply. The objective is to produce a datatable that contains the product of two values from two different datatables. The twist is, the CurveFitTable came from different csv files in a directory which I already defined.
What I intended to do is to have a datatable like the adjustedCopy table in the image below. Unfortunately, what I'm getting is a single datatable which kept on being overwritten and whenever I attempt to databind it to the grid, the datatable seems to be empty. Please help. T.T
This is my code:
Dim adjusteddemandtable As New DataTable()
Dim adjustedcopy As New DataTable()
Dim multiply_SF As Double
Dim adjusted_Demand As Double
Dim initial_Demand As Double
Dim basecurvestamp As Date
adjusteddemandtable.Columns.Add("Base Curve", GetType(Date))
adjusteddemandtable.Columns("Base Curve").SetOrdinal(0)
adjusteddemandtable.Columns.Add("Adjusted_Demand", GetType(Double))
Dim CurveFitTatble As New DataTable()
Try
For Each filename As String In System.IO.Directory.GetFiles(BackUpDirectory)
CurveFitTatble = GetDataTabletFromCSVFile(filename)
For Each row2 As DataRow In dtSF.Rows()
For Each row As DataRow In CurveFitTatble.Rows()
initial_Demand = row(1)
basecurvestamp = row(0)
multiply_SF = row2(0)
adjusted_Demand = multiply_SF * initial_Demand
Dim rowa As DataRow = adjusteddemandtable.Rows.Add()
rowa(0) = basecurvestamp
rowa(1) = adjusted_Demand
Next
Next
adjustedcopy.Merge(adjusteddemandtable, True, MissingSchemaAction.AddWithKey)
Next
GridView1.DataSource = adjustedcopy
GridView1.DataBind()
Catch ex As Exception
ErrorMessageBox(ex.Message)
End Try
I think, I'm missing something or overlooked an important step. Please advise. Thanks in advance.
I just did what #jmcilhinney told me (that is to replace nested Foreach nested loop. Here is my new code (and fortunately, it is working as its expected output requires)
Try
Dim y As Integer = System.IO.Directory.GetFiles(BackUpDirectory).Length
Dim row1 As DataRow
Dim i As Integer = 0
While i < y
row1 = dtSF.Rows(i)
Dim filenames As String() = System.IO.Directory.GetFiles(BackUpDirectory)
Dim filename As String = filenames(i)
multiply_SF = row1(0)
CurveFitTatble = New DataTable()
Dim TS_Name As String = "TmeStamp" + "_" & i
Dim AD_Name As String = "AdjustedDemand" + "_" & i
If i = 0 Then
adjusteddemandtable.Columns.Add(TS_Name, GetType(Date))
adjusteddemandtable.Columns(TS_Name).SetOrdinal(i)
adjusteddemandtable.Columns.Add(AD_Name, GetType(Double))
adjusteddemandtable.Columns(AD_Name).SetOrdinal(i + 1)
ElseIf i > 0 Then
adjusteddemandtable.Columns.Add(TS_Name, GetType(Date))
adjusteddemandtable.Columns(TS_Name).SetOrdinal(i + 1)
adjusteddemandtable.Columns.Add(AD_Name, GetType(Double))
adjusteddemandtable.Columns(AD_Name).SetOrdinal(i + 2)
End If
'If row1(0) = filename Then
CurveFitTatble = GetDataTabletFromCSVFile(filename)
'For Each row As DataRow In CurveFitTatble.Rows()
Dim row As DataRow
For j As Integer = 0 To CurveFitTatble.Rows.Count - 1
row = CurveFitTatble.Rows(j)
initial_Demand = row(1)
basecurvestamp = row(0)
adjusted_Demand = multiply_SF * initial_Demand
Dim rowa As DataRow = adjusteddemandtable.Rows.Add()
If i = 0 Then
rowa(i) = basecurvestamp
rowa(i + 1) = adjusted_Demand
ElseIf i > 0 Then
rowa(i + 1) = basecurvestamp
rowa(i + 2) = adjusted_Demand
End If
Next
i = i + 1
End While
Catch ex As Exception
ErrorMessageBox(ex.Message)
End Try

Adding Rows dynamically to GridView in ASP.NET

I use the following code for adding Rows dynamically to GridView in ASP.NET.
ViewState("CourseFeesDetails") = dsGetStudentCourseFeesDetail
If ViewState("CourseFeesDetails") IsNot Nothing Then
Dim dtCourseTable As New DataTable
dtCourseTable = CType(ViewState("CourseFeesDetails"), DataTable)
Dim drCurrentRow As DataRow = Nothing
Dim txtFeesAmt As New System.Web.UI.WebControls.TextBox
If dtCourseTable.Rows.Count > 0 Then
For i = 1 To nNoOfInstallments
drCurrentRow = dtCourseTable.NewRow()
i = i + 1
dtCourseTable.Rows.Add(drCurrentRow)
Next
ViewState("CourseFeesDetails") = dtCourseTable
gvCourseFeeDisplay.DataSource = dtCourseTable
gvCourseFeeDisplay.DataBind()
End If
End If
It does add the number of rows, but adds 1 extra. What should I do?

ASP.NET DataSet loses data?

The following code adds, dynamically, tabs to a tab control. In each row of datatable, it calls a function CarregaAvaliacoes that adds some controls to each tab. Dataset dtGruposCompetencias has 4 rows. However, after first row (after calling CarregaAvaliacoes), it looses data (IndexOutOfRangeException). Any ideias?
EDIT: I have debugged it, followed each step. After finishing CarregaAvaliacoes first time, dataset has no longer data. Don't understand why.
Thanks!
Dim dtGruposCompetencias As New DataTable
Dim nivel As Integer = 4
dtGruposCompetencias = dal.CarregaAvaliacoesPorNivel(nivel)
lblTemp.Text = dtGruposCompetencias.Rows.Count
If dtGruposCompetencias.Rows.Count > 0 Then
Dim tb As AjaxControlToolkit.TabPanel
For i As Integer = 0 To dtGruposCompetencias.Rows.Count - 1
tb = New AjaxControlToolkit.TabPanel
tb.HeaderText = dtGruposCompetencias.Rows(i)("designacao").ToString()
Dim grupo As String = dtGruposCompetencias.Rows(i)("idGrupoCompetencia").ToString()
CarregaAvaliacoes(tb, grupo)
Next
tabAvaliacoes.ActiveTabIndex = 0
Else
lblSemAvaliacoes.Visible = True
End If
Public Sub CarregaAvaliacoes(tab As AjaxControlToolkit.TabPanel, idGrupoCompetencia As String)
Dim dtAtributos As New DataTable
dtAtributos = dal.CarregaAtributosAvaliacao(idGrupoCompetencia)
If dtAtributos.Rows.Count > 0 Then
Dim tdCompetencia As TableCell, tdAtributo As TableCell
Dim tr As TableRow
Dim tblAvaliacao As New Table
For i As Integer = 0 To dtAtributos.Rows.Count - 1
tdCompetencia = New TableCell
tdCompetencia.Controls.Add(fRetTexto(dtAtributos.Rows(i)("competencia").ToString()))
tdAtributo = New TableCell
tdAtributo.Controls.Add(fRetTexto(dtAtributos.Rows(i)("atributo").ToString()))
tr = New TableRow
tr.Cells.Add(tdCompetencia)
tr.Cells.Add(tdAtributo)
tblAvaliacao = New Table
tblAvaliacao.Rows.Add(tr)
Next
tab.Controls.Add(tblAvaliacao)
Else
Dim lblSemRegistos As New Label
lblSemRegistos.Text = "Sem dados para avaliação."
tab.Controls.Add(lblSemAvaliacoes)
End If
End Sub

Dynamic tables with hyperlinks

I want to build a table based on data in a recordset with a hyperlink that calls a method which takes the message id from the link and redirects to another page to view the message. I've tried HTMLAnchors, LinkButtons but none seem to work.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim iUserID As Integer
Dim sMsgId, sMsgSubject, sMsgDateSent, sMsgRead, sMsgFrom, sFirstname As String
iUserID = Session("UserID")
Dim cn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString)
Dim cmd As New SqlCommand()
Dim tblRow As TableRow
Dim tblCell1 As TableCell
Dim tblCell2 As TableCell
Dim tblCell3 As TableCell
Dim tblCell4 As TableCell
tblRow = New TableRow
tblCell1 = New TableCell
tblCell1.Text = ""
tblRow.Cells.Add(tblCell1)
tblCell2 = New TableCell
tblCell2.Text = "From"
tblRow.Cells.Add(tblCell2)
tblCell3 = New TableCell
tblCell3.Text = "Subject"
tblRow.Cells.Add(tblCell3)
tblCell4 = New TableCell
tblCell4.Text = "Received"
tblRow.Cells.Add(tblCell4)
tblMessages.Rows.Add(tblRow)
tblCell1 = Nothing
tblCell2 = Nothing
tblCell3 = Nothing
tblRow = Nothing
cmd.CommandText = "SELECT msg.msg_id, msg.msg_usr_from, usr.usr_firstname, msg.msg_subject, msg.msg_date_sent, msg.msg_read FROM messages msg inner join users usr on msg.msg_usr_to = usr.usr_id WHERE msg.msg_usr_to='" & iUserID & "'"
cmd.Connection = cn
cn.Open()
Dim r As SqlDataReader = cmd.ExecuteReader
Dim objControl As Button
While r.Read()
tblCell1 = New TableCell
tblCell2 = New TableCell
tblCell3 = New TableCell
tblCell4 = New TableCell
tblRow = New TableRow
sMsgId = CStr(r("msg_id"))
sMsgFrom = CStr(r("msg_usr_from"))
sFirstname = CStr(r("usr_firstname"))
sMsgSubject = CStr(r("msg_subject"))
sMsgDateSent = CStr(r("msg_date_sent"))
sMsgRead = CStr(r("msg_read"))
If sMsgRead = "N" Then
tblCell1.Text = "<img width=30px src=images/new_email.png>"
Else
tblCell1.Text = "<img width=30px src=images/email_open.png>"
End If
Dim objLinkbutton As
tblRow.Cells.Add(tblCell1)
tblCell2.Text = sFirstname
tblRow.Cells.Add(tblCell2)
tblCell3.controls.add( HYPERLINK HERE!!!
tblRow.Cells.Add(tblCell3)
tblCell4.Text = FormatDateTime(sMsgDateSent, DateFormat.LongDate)
tblRow.Cells.Add(tblCell4)
If sMsgRead = "N" Then
tblCell2.Font.Bold = True
tblCell3.Font.Bold = True
tblCell4.Font.Bold = True
End If
tblMessages.Rows.Add(tblRow)
tblCell1 = Nothing
tblCell2 = Nothing
tblCell3 = Nothing
tblCell4 = Nothing
tblRow = Nothing
End While
r.Close()
cn.Close()
cn = Nothing
r = Nothing
cmd = Nothing
End Sub
Protected Sub viewmessage(messageid As String)
Context.Items("messageid") = "messageid"
Server.Transfer("viewmessage.aspx")
End Sub
End Class
You have two choices, as I see it.
Massage the data set so it contains the complete URI, with the ID, and then bind to a grid
Use a routine to create the URI and then bind the grid via row binding
Continuing hand binding the table, using hyperlink controls
All are easy enough to do, but the #3 option is the least "ASP.NET" like. I am not fond of hand building tables, when I can do the same with a control, simply by either a) altering the data before bind or b) stopping on the row binding event and adding information.
Use query string to store the id value of the selected link and redirect to the page you want.
try something like this:
Dim a As New HyperLink()
a.Text = "your text"
a.NavigateUrl = "yourPage.aspx?idMsg=" + r["msg_id"]
tblCell3.controls.add(a)

compare the content of 2 gridviews in one aspx page

we need to compare the content of 2 identical gridviews and extract rows that differ in a third gridview, is this doable?
i tried a lot but faced no luck, please help me.
at our office we take daily backup of ASP.net application ms access backend
for the next few days we need to evaluate the changes made to records in the database tables
at the end of each day i want to compare 2 access databases first database is the backup of yesterday and second database is the backup of today
i thought of the following algorithm, please read carefully and tell me how to proceed to compare the datatables / gridviews
i need to display th rows / cells containing the differences / updates / deleted data
comparing two ms access backend databases of an asp.net web application
my team and myseld figured it out
Imports System.Data
Imports System.Data.OleDb
Partial Class MoKoTrack
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myDB = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|databaseName.mdb;Persist Security Info=True")
Session("CurrentDB") = myDB
myDB.open()
Dim mytables = myDB.GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys, New Object() {})
Dim CurrentTable As String
For i = 1 To mytables.Rows.Count
CurrentTable = mytables.Rows(i - 1).Item(2).ToString
If CurrentTable.Contains("Backup") Then CompareTable(CurrentTable, mytables.Rows(i - 1).Item(3).ToString)
Next i
' Dim myGrid As New GridView
'myGrid.DataSource = mytables
'myGrid.DataBind()
'Me.Form.Controls.Add(myGrid)
'myDB.Close()
End Sub
Sub CompareTable(ByVal BackupTableName As String, ByVal myPrimKey As String)
Dim OriginalTable As New DataTable
Dim BackupTable As New DataTable
Dim ModificationsTable As New DataTable
Dim AddedTable As New DataTable
Dim DeletedTable As New DataTable
Dim myDB = Session("CurrentDB")
Dim FinalSQLString = "SELECT * FROM [" + BackupTableName + "]"
Dim myDBCommand = New OleDbCommand(FinalSQLString, myDB)
Dim myReader As IDataReader = myDBCommand.ExecuteReader()
BackupTable.Load(myReader)
Dim OriginalTableName = Left(BackupTableName, Len(BackupTableName) - 6)
Dim FinalSQLString2 = "SELECT * FROM [" + OriginalTableName + "]"
Dim myDBCommand2 = New OleDbCommand(FinalSQLString2, myDB)
'Generate a temporary reader to get the number of cases
Dim myReader2 As IDataReader = myDBCommand2.ExecuteReader()
OriginalTable.Load(myReader2)
Dim myPrimColumn(0) As DataColumn
myPrimColumn(0) = OriginalTable.Columns(myPrimKey)
OriginalTable.PrimaryKey = myPrimColumn
Dim myPrimColumn2(0) As DataColumn
myPrimColumn2(0) = BackupTable.Columns(myPrimKey)
BackupTable.PrimaryKey = myPrimColumn2
AddedTable = OriginalTable.Clone
DeletedTable = OriginalTable.Clone
ModificationsTable = OriginalTable.Clone
ModificationsTable.PrimaryKey = Nothing
Dim CurrentVal As String
For i = 0 To OriginalTable.Rows.Count - 1
CurrentVal = OriginalTable.Rows(i).Item(myPrimKey).ToString
Dim foundRow As DataRow = BackupTable.Rows.Find(CurrentVal)
If foundRow IsNot Nothing Then
For t = 0 To OriginalTable.Columns.Count - 1
If Not foundRow.Item(t).ToString = OriginalTable.Rows(i).Item(t).ToString Then
ModificationsTable.ImportRow(OriginalTable.Rows(i))
'ModificationsTable.Rows(ModificationsTable.Rows.Count - 1).Item(t) = ModificationsTable.Rows(ModificationsTable.Rows.Count - 1).Item(t) & "Modified"
ModificationsTable.ImportRow(foundRow)
End If
Next
Else
AddedTable.ImportRow(OriginalTable.Rows(i))
End If
Next
For i = 0 To BackupTable.Rows.Count - 1
CurrentVal = BackupTable.Rows(i).Item(myPrimKey).ToString
Dim foundRow As DataRow = OriginalTable.Rows.Find(CurrentVal)
If foundRow Is Nothing Then
DeletedTable.ImportRow(OriginalTable.Rows(i))
End If
Next
If AddedTable.Rows.Count > 0 Then
Dim myLabel As New Label
myLabel.Text = "<br/> The following records were added to table " & OriginalTableName & "<br/> <br/>"
Me.form1.Controls.Add(myLabel)
Dim myGrid As New GridView
myGrid.DataSource = AddedTable
myGrid.DataBind()
Me.form1.Controls.Add(myGrid)
End If
If ModificationsTable.Rows.Count > 0 Then
Dim myLabel As New Label
myLabel.Text = "<br/> The following records were modified in table " & OriginalTableName & "<br/> <br/>"
Me.form1.Controls.Add(myLabel)
Dim myGrid As New GridView
myGrid.DataSource = ModificationsTable
myGrid.DataBind()
Me.form1.Controls.Add(myGrid)
End If
If DeletedTable.Rows.Count > 0 Then
Dim myLabel As New Label
myLabel.Text = "<br/> The following records were deleted from table " & OriginalTableName & "<br/> <br/>"
Me.form1.Controls.Add(myLabel)
Dim myGrid As New GridView
myGrid.DataSource = DeletedTable
myGrid.DataBind()
Me.form1.Controls.Add(myGrid)
End If
End Sub
End Class
I usually compare it using LINQ
This will give you a head start
var SearchResults1 = from u in YourDB.YourUserTable1
orderby u.YourColumn
select u;
GridView1.DataSource = SearchResults1;
GridView1.DataBind();
var SearchResults2 = from u in YourDB.YourUserTable2
orderby u.YourColumn
select u;
GridView2.DataSource = SearchResults2;
GridView2.DataBind();
var SearchResults3 = from u1 in SearchResults2
where !(from u2 in SearchResults1
select u2.YourTablePrimaryKey).Contains(u1.YourTablePrimaryKey)
orderby u1.YourColumn
select u1;
GridView3.DataSource = SearchResults3;
GridView3.DataBind();
You can further elaborate SearchResults 3 if you want like comparing whats in SearchResults1 but not in SearchResults2 and vice versa

Resources