Using SqlBulkCopy SqlRowsCopied to update a label - asp.net

I have a simple web application that is reading records from a CSV file and storing them in a database table. Then I am using SqlBulkCopy to copy the records into an SQL database using batches. All is fine with the insert. I am trying to give the user some feedback using OnSqlRowsCopied and NotifyAfter. The goal is to update a label that is contained in an UpdatePanel to display the number of records copied at the current NotifyAfter interval. However, the label will not update until SqlBulkCopy has complete. I can see that the s_OnSqlRowsCopied event is firing using Debug.WriteLine. What is the reason why the label won't update and how can I overcome this?
Code Behind
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Dim filePath As String
Dim rowsCopied As String
Public Sub btnGetCSV_Click(sender As Object, e As EventArgs) Handles btnGetCSV.Click
filePath = System.IO.Path.GetFullPath(fileUpload1.PostedFile.FileName)
lblInfo.Text = filePath
End Sub
Protected Sub btnToSQL_Click(sender As Object, e As EventArgs) Handles btnToSQL.Click
Dim cs As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("csMediaPortal").ConnectionString
CopyData(CSVtoDataTable(lblInfo.Text.ToString()), cs)
End Sub
Private Function CSVtoDataTable(filePath As String) As DataTable
Dim dt As DataTable = Nothing
Dim sourcePath As String = String.Empty
Dim csvFile As String = String.Empty
Dim conString As String = String.Empty
Dim conn As OleDb.OleDbConnection = Nothing
Dim adapter As OleDb.OleDbDataAdapter = Nothing
Dim selString As String = String.Empty
Try
sourcePath = System.IO.Path.GetDirectoryName(filePath)
csvFile = System.IO.Path.GetFileName(filePath)
conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sourcePath & ";Extended Properties=""text;HDR=No;FMT=FixedLength"""
conn = New OleDb.OleDbConnection(conString)
selString = "Select * From " & csvFile
adapter = New OleDb.OleDbDataAdapter(selString, conn)
dt = New DataTable(System.IO.Path.GetFileNameWithoutExtension(filePath))
conn.Open()
adapter.Fill(dt)
conn.Close()
Catch ex As Exception
lblInfo.Text = ex.Message
Finally
adapter.Dispose()
conn.Dispose()
End Try
Return dt
End Function
Protected Sub CopyData(sourceTable As DataTable, cs As String)
Using s As SqlBulkCopy = New SqlBulkCopy(cs, SqlBulkCopyOptions.UseInternalTransaction)
s.DestinationTableName = "test"
s.BatchSize = 1000
Try
AddHandler s.SqlRowsCopied, AddressOf s_OnSqlRowsCopied
s.NotifyAfter = 900
s.WriteToServer(sourceTable)
Catch ex As Exception
DirectCast(DirectCast(HttpContext.Current.Handler, Page).FindControl("lblInfo"), Label).Text = "Commit Error: " & ex.Message
End Try
s.Close()
End Using
End Sub
Protected Sub s_OnSqlRowsCopied(sender As Object, e As SqlRowsCopiedEventArgs)
Me.lblProgress.Value = e.RowsCopied.ToString()
Me.UpdatePanel1.Update()
Debug.WriteLine(e.RowsCopied)
End Sub
End Class
Web Form
<%# Page Language="vb" CodeBehind="WebForm1.aspx.vb" Inherits="CSVUpload.WebForm1" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:FileUpload ID="fileUpload1" runat="server" />
<asp:Button ID="btnGetCSV" runat="server" Text="Post" OnClick="btnGetCSV_Click" />
<asp:Label ID="lblInfo" runat="server" Text="Label"></asp:Label>
</div>
<asp:Button ID="btnToSQL" runat="server" Text="Insert To SQL" OnClick="btnToSQL_Click" />
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<input runat="server" type="text" id="lblProgress" value="0" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

Related

How to update a 'second' GridView based on an update on 'first' GridView?

I'm creating an web form more complicated than what I'm listing below.
Bottom line, I'm updating a GridView and I'd like to have the 2nd Gridview update itself based on the GridView change made on the first one!
Please, help! I've already tried including a GridView2.DataBind() line in the updateNumber() method, but I receive an instance reference error or something like that. I've been stuck on this for days!
I update the number in GridView1, it refreshes and updates with new number, but NOT GridView2, which should display the updated number on GridView1 (and yes, I do need GridView2 to call GetUpdateNumber() GridView2 still shows the old number... the ones prior to updating GridView1
Here is the code behind
Option Strict On
Imports Team.MyWebProject
Public Class Blah
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myNumberClass As New myClass1
Dim mylist As New List(Of myClass1)
Try
If (Not IsPostBack) Then
If Session("Number") Is Nothing Then
myNumberClass.number = 2
mylist.Add(myNumberClass)
Session("Number") = mylist
Else
myNumberClass = Nothing
mylist = CType(Session("Number"), List(Of myClass1))
End If
Else
mylist = CType(Session("Number"), List(Of myClass1))
End If
Catch ex As Exception
MsgBox(ex.Message & "/Blah.aspx.vb Page_Load")
End Try
End Sub
Public Function getNumber() As List(Of myClass1)
Try
Dim mylist As List(Of myClass1) = CType(Session("Number"), List(Of myClass1))
Return mylist
Catch ex As Exception
MsgBox(ex.Message & "/Blah.aspx.vb GetNumber")
End Try
End Function
Public Function getUpdatedNumber() As List(Of myClass1)
Try
Dim mylist As List(Of myClass1) = CType(Session("Number"), List(Of myClass1))
Return mylist
Catch ex As Exception
MsgBox(ex.Message & "/Blah.aspx.vb GetUpdateNumber")
End Try
End Function
Public Sub updateNumber(ByVal number As Integer)
Try
Dim mylist As List(Of myClass1) = CType(Session(Number"), List(Of myClass1))
Dim LoopIndex As New Integer
LoopIndex = 0
For Each Match In mylist
mylist(LoopIndex).Number = number
LoopIndex += 1
Next
Session("Number") = mylist
Catch ex As Exception
MsgBox(ex.Message & "blah.aspx.vb updateNumber")
End Try
End Sub
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
End Sub
Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles GridView1.RowEditing
End Sub
Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Try
Catch ex As Exception
MsgBox(ex.Message & "Blah.aspx.vb GridView13_RowUpdating()")
End Try
End Sub
Protected Sub GridView2_DataBinding(sender As Object, e As EventArgs) Handles GridView2.DataBinding
Try
Dim mylist As List(Of myClass1) = CType(Session("Number"), List(Of myClass1))
getUpdatedNumber()
Catch ex As Exception
MsgBox(ex.Message & "Blah.aspx GridView2_Databinding()")
End Try
End Sub
End Class
Here is the myClass1.vb class:
Namespace MyWebProject
Public Class myClass1
Private _number As Integer
Public Sub New()
_number = 0
End Sub
Public Sub New(ByVal number As Integer)
_number = number
End Sub
Public Property Number() As Integer
Get
Return _number
End Get
Set(value As Integer)
_number = value
End Set
End Property
End Class
End Namespace
And here is the MarkUp page:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Blah.aspx.vb" Inherits="GridViewUpdateTest.Blah" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getNumber" TypeName="GridViewUpdateTest.Blah" UpdateMethod="updateNumber">
<UpdateParameters>
<asp:Parameter Name="number" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource2">
<Columns>
<asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="getUpdatedNumber" TypeName="GridViewUpdateTest.Blah"></asp:ObjectDataSource>
</form>
</body>
</html>

display image from db alongside other webpage elements

A continuation of the question on display (not download) image from db
<%# Page Language="VB" AutoEventWireup="false" CodeFile="imgTest1.aspx.vb" Inherits="imgTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="background-color: aliceblue;">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<br />
<br />
</div>
<div style="background-color: burlywood;">
<asp:Image ID="Image1" runat="server" ImageUrl="imgTest1.aspx?id=1" />
<br />
<br />
<br />
</div>
</div>
</form>
</body>
</html>
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class imgTest
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Request.QueryString("id") IsNot Nothing Then
Dim strQuery As String = "select name, contentType, data from [imageTest] where id=1"
Dim cmd As SqlCommand = New SqlCommand(strQuery)
cmd.Parameters.Add("#id", SqlDbType.Int).Value = Convert.ToInt32("1")
Dim dt As DataTable = GetData(cmd)
If dt IsNot Nothing Then
Dim bytes() As Byte = CType(dt.Rows(0)("data"), Byte())
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = dt.Rows(0)("ContentType").ToString()
Response.AddHeader("content-disposition", "filename=" & dt.Rows(0)("name").ToString())
Response.BinaryWrite(bytes)
Response.Flush()
Response.End()
End If
End If
End Sub
Public Function GetData(ByVal cmd As SqlCommand) As DataTable
Dim dt As New DataTable
Dim strConnString As String = System.Configuration.ConfigurationManager.ConnectionStrings("CapstoneConnectionString1").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim sda As New SqlDataAdapter
cmd.CommandType = CommandType.Text
cmd.Connection = con
Try
con.Open()
sda.SelectCommand = cmd
sda.Fill(dt)
Return dt
Catch ex As Exception
Response.Write(ex.Message)
Return Nothing
Finally
con.Close()
sda.Dispose()
con.Dispose()
End Try
End Function
End Class
I can now display the image on the aspx page but only the image is being displayed, all other web elements are missing.
I can only think of one reason for this, given that following are true:
Page used to render contents is imgTest1.aspx as well as page used to return image is also imgTest1.aspx.
imgTest1.aspx is always invoked with query parameter, id.
because of this page always returns an image.
Can you change the code as follows and tell us how it works:
...
...
<asp:Image ID="Image1" runat="server" ImageUrl="imgTest1.aspx?imageId=1" />
...
...
and in code-behind
If Request.QueryString("imageId") IsNot Nothing Then
Dim strQuery As String = "select name, contentType, data from [imageTest] where id=1"
Dim cmd As SqlCommand = New SqlCommand(strQuery)
cmd.Parameters.Add("#id", SqlDbType.Int).Value = Convert.ToInt32("1")

display uploded image in image control

i want to upload an image to a folder which is in my website's folder and then save it's path to database and display the uploaded image in control image.
when i executed my page the image is uploaded succefully and it's saved to database but it dosent displayed on the image control.
this is my view :
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:Image ID="Image1" runat="server" Height="91px" Width="145px" />
</form>
<p>
</p>
</body>
</html>
and this is my code :
Imports System.Data.SqlClient
Imports System.IO
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim cn As SqlConnection
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If FileUpload1.PostedFile IsNot Nothing Then
Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
'Save files to disk
FileUpload1.SaveAs(Server.MapPath("~/im/" & FileName))
'Add Entry to DataBase
Dim strQuery As String = "insert into dbo.images" & " values(#FileName, #FilePath)"
Dim cmd As New SqlCommand(strQuery)
cmd.Parameters.AddWithValue("#FileName", FileName)
cmd.Parameters.AddWithValue("#FilePath", "~/im/" & FileName)
cmd.CommandType = CommandType.Text
cmd.Connection = cn
Try
cn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message)
Finally
cn.Close()
cn.Dispose()
End Try
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
cn = New SqlConnection("server=SEVO-PC;initial catalog=controle;integrated security=true")
End Sub
Protected Sub FileUpload1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileUpload1.Load
Image1.ImageUrl = "im/" & FileUpload1.FileName
End Sub
End Class
Try the following edit
Protected Sub FileUpload1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles FileUpload1.Load
Image1.ImageUrl = Server.MapPath("/im/") & FileUpload1.FileName
End Sub

Dynamically created buttons not calling function on postback

In my visual basic web application I have a list of generated buttons that are supposed to allow the download of a file on click.
I had the example working with generated buttons on pageload, but all of a sudden the download function stopped getting called on post back, and now all the button click does (for any of the buttons) is cause a page post back.
My code:
Public folder As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
folder = "Main"
PopulateFiles(folder)
End If
End Sub
Protected Sub PopulateFiles(ByVal folder As String)
Dim myConnection As SqlConnection
Dim conString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim myCommand As SqlCommand
Dim myDataReader As SqlDataReader
Dim text As String
Dim size As Decimal
Dim name As String
Dim type As String
Dim id As Integer
folderName.Text = folder
container.Controls.Clear()
myConnection = New SqlConnection(conString)
myConnection.Open()
myCommand = New SqlCommand("Uploads_GetAllFiles", myConnection)
myCommand.CommandType = CommandType.StoredProcedure
myCommand.Parameters.AddWithValue("#folder", folder)
Try
myDataReader = myCommand.ExecuteReader()
If myDataReader.HasRows Then
Do While myDataReader.Read()
name = myDataReader.Item("Name")
type = myDataReader.Item("Type")
id = myDataReader.Item("File_ID")
size = Math.Round(myDataReader.Item("Size") / 1000, 2)
container.Controls.Add(New LiteralControl("<div class='newRow'>"))
text = "<div class='fileName'>" & name & "</div>"
container.Controls.Add(New LiteralControl(text))
text = "<div class='fileType'>" & type & "</div>"
container.Controls.Add(New LiteralControl(text))
text = "<div class='fileSize'>" & size.ToString() & "kb</div>"
container.Controls.Add(New LiteralControl(text))
container.Controls.Add(New LiteralControl("<div class='fileDownload'>"))
Dim newBtn As New Button
newBtn.ID = "link" & id
newBtn.Text = "Download"
newBtn.CssClass = "newbie"
AddHandler newBtn.Click, AddressOf Retreive_Doc
newBtn.CommandArgument = id
container.Controls.Add(newBtn)
container.Controls.Add(New LiteralControl("</div>"))
container.Controls.Add(New LiteralControl("<div class='fileDelete'>"))
Dim newDelBtn As New Button
newDelBtn.ID = "delete" & id
newDelBtn.Text = "Delete"
newDelBtn.CssClass = "delBtn"
AddHandler newDelBtn.Click, AddressOf Retreive_Xls
newDelBtn.CommandArgument = id
container.Controls.Add(newDelBtn)
container.Controls.Add(New LiteralControl("</div>"))
container.Controls.Add(New LiteralControl("</div>"))
Loop
End If
Catch ex As Exception
MsgBox(ex.ToString())
Finally
myConnection.Close()
End Try
End Sub
Protected Sub Retreive_Doc(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
Dim button As Button = sender
Dim id As Integer = button.CommandArgument
Dim cmd As SqlCommand = New SqlCommand("Uploads_GetFile")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("#id", id)
Dim dt As DataTable = GetData(cmd)
If dt IsNot Nothing Then
download(dt)
End If
End Sub
I could show the functions called from this function, but the initial function isn't even being called so I'm not sure there is a point.
My HTML is as follows:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:FileUpload ID="upload1" runat="server" /><asp:Button ID="test1" runat="server" Text="Upload" />
<asp:TextBox ID="folderTag" runat="server" ></asp:TextBox>
<asp:Button ID="search" runat="server" Text="Search" />
<asp:Label ID="folderName" runat="server">General</asp:Label><br />
<div id="navContainer" runat="server">
</div>
<div id="hiddenContent">
<asp:LinkButton ID="LinkButton1" CssClass="hide" runat="server" OnClick = "Retreive_Doc">Download Doc</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" CssClass="hide" runat="server" OnClick = "Retreive_Xls">Download xls</asp:LinkButton>
</div>
<div id="container" runat="server">
</div>
</form>
As I said before. This was working a few days ago and through the course of adding some other functionality somehow I lost it.
I'm unsure of why it posts back without calling any function.
Thanks for any help!
You need to recreate the controls with same ID in post back if you created them dynamically. Otherwise, they will disappear after postback.
Remove If Not IsPostBack Then in page load event.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
folder = "Main"
PopulateFiles(folder)
End Sub
Another thought:
Is there a reason not to use CommandEvent instead of Click event if you want CommandArgument?
Again, Is there a reason not to use PlaceHolder instead of <div id="container" runat="server">?
AddHandler newBtn.Command, AddressOf Retreive_Doc
newBtn.CommandArgument = id
....
protected void Retreive_Doc(object sender, CommandEventArgs e)

dynamically build html table on page load

Edit for vhinn
I want it to look like this:
I am trying to build an html table dynamically on pageload with variables from a database.
this is an example strictly html http://jsfiddle.net/jdv590/daCum/1/
code:
Private Sub brothersgird()
Dim html As New StringBuilder
Dim sql As String = "select Name, Hometown, Picture, Class from brothers",
connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=~/App_Data/Members.accdb;Persist Security Info=False;",
conn As New OleDbConnection(connstring),
myCommand As New OleDbCommand(sql, conn),
namevar As String,
classvar As String,
hometownvar As String
Dim x As Integer = 1
conn.Open()
Dim dr As OleDbDataReader = myCommand.ExecuteReader
html.Append("<table>")
Do While dr.Read
' imagevar = dr("Picture")
namevar = dr("Name")
classvar = dr("Class")
hometownvar = dr("Hometown")
html.Append("<tr>")
Do While x < 4
html.Append("<td><p>" & namevar & "<br /> Hometown: " & hometownvar & "<br /> Class: " & classvar & "</p></td>")
x = x + 1
Loop
html.Append("</tr>")
x = 0
Loop
html.Append("</table>")
dr.Close()
conn.Close()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
brothersgird()
'write to panel maybe with this idea:
seniorpanel.html=html ???
End Sub
aspx side:
<asp:Panel ID="seniorpanel" runat="server">
</asp:Panel>
in your markup:
<asp:Panel ID="seniorpanel" runat="server">
<asp:GridView ID="brothersgird" runat="server" ShowHeader="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<p>
<%# Eval("Name")%><br />
Hometown:
<%# Eval("Hometown")%><br />
Class:
<%# Eval("Hometown")%>
</p>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:Panel>
code-behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
brothersgird.DataSource = SelectBrothers()
brothersgird.DataBind()
End If
End Sub
Private Function SelectBrothers() As DataTable
Dim sql As String = "select Name, Hometown, Picture, Class from brothers"
Dim connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=~/App_Data/Members.accdb;Persist Security Info=False;"
Dim conn As New OleDbConnection(connstring)
Dim ds As New DataSet
Dim adapter As New OleDbDataAdapter()
adapter.SelectCommand = New OleDbCommand(sql, conn)
adapter.Fill(ds)
Return ds.Tables(0)
End Function
You familiar with DataGrids?
Client Side:
<asp:DataGrid runat="server" id="dataTable">
</asp:DataGrid>
Server Side:
//Get your data table from the database - let's say the variable is called dt
dataTable.DataSource = dt
dataTable.DataBind()
There is a lot more you can do with datagrids, but this should be enough to get you started if you decide to go this route.
Are you asking how to do this in javascript if you already have the data? If so an example would be kinda like this:
var row = document.createElement("TR");
var th1 = document.createElement("TH");
row.appendChild(th1);

Resources