PostgreSQL strange UPDATE time 2? - postgresql

As I reported in my previous post, I'm faced with a problem running my VB.NET application with POSTGRESQL 9.6 and Npgsql 3.2.6 on WIN10. The problem is a strange long time required to perform some DML statements on a table with 250 rows (normally few ms but sometime 0.8s!).
As Laurenz Albe suggested in response to my previous post, I activated the log on PostgreSQL but no statements are logged when the strange behavior occurs.
The PostgreSQL DB server is connected to the main pc by a GB lan.
In my application I have a separate thread in which I perform my tasks (including activities on DB).
In this thread i call the update this way:
Dim vT As DateTime = Now
'apertura lavorazione
vRet = mMaster.DB.apriLavorazione(vPallet)
Dim vS As String = (Now - vT).Seconds.ToString + ":" + (Now - vT).Milliseconds.ToString
The update function is something like this:
Function apriLavorazione(ByRef pPallet As clsPallet) As clsCallRes
Dim vObjRet As clsCallRes = New clsCallRes
Dim vConn As NpgsqlConnection = Nothing
Dim vCommand As NpgsqlCommand = Nothing
Dim vPar As NpgsqlParameter
Try
'creo la connessione
vConn = New NpgsqlConnection(mConnectionString)
vConn.Open()
pPallet.intermedio1 = Now
'creo il comando dalla connessione ed associo la transazione
vCommand = vConn.CreateCommand()
pPallet.WPAL_DATA_INI = DateTime.Now()
vCommand.CommandText = "UPDATE work.wip_pallets SET WPAL_DATA_INI = #dataini" +
" ,STAZ_ID = #stazid, WPAL_PRESENZA_CEPA = #presenzacepa, WPAL_SCARTO = #scarto" +
" WHERE CEPA_CODICE = #cepacodice;"
vPar = New NpgsqlParameter
vPar.ParameterName = "dataini"
vPar.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Timestamp
vPar.Value = pPallet.WPAL_DATA_INI.ToString("yyyy-MM-dd HH:mm:ss.fff")
vCommand.Parameters.Add(vPar)
vPar = New NpgsqlParameter
vPar.ParameterName = "stazid"
vPar.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Integer
vPar.Value = pPallet.STAZ_ID
vCommand.Parameters.Add(vPar)
vPar = New NpgsqlParameter
vPar.ParameterName = "presenzacepa"
vPar.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Integer
vPar.Value = pPallet.WPAL_PRESENZA_CEPA
vCommand.Parameters.Add(vPar)
vPar = New NpgsqlParameter
vPar.ParameterName = "scarto"
vPar.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Integer
vPar.Value = pPallet.WPAL_SCARTO
vCommand.Parameters.Add(vPar)
vPar = New NpgsqlParameter
vPar.ParameterName = "cepacodice"
vPar.NpgsqlDbType = NpgsqlTypes.NpgsqlDbType.Varchar
vPar.Value = pPallet.CEPA_CODICE
vCommand.Parameters.Add(vPar)
vCommand.Prepare()
vCommand.ExecuteNonQuery()
Catch ex As Exception
vObjRet.err = True
vObjRet.message = String.Concat("apriLavorazione:", ex.Message)
Finally
vConn.Close()
End Try
Return vObjRet
End Function
I don't think is a code that can require about one second!
Do you have any idea what the problem might be?
1) npgsql on thread?
2) network latency?
3) bad code for multithreaded application?
4) ????

I've moved the DB on the same machine as the application that use it. At this time (and i hope for the future) the strange update time never occurred again. I suspect something related to network latency.
Thanks for your time reading this post.

Related

Updated Yahoo Weather API - .NET

I'm trying to implement the newest yahoo weather API in a .NET application (the one we were using was discontinued in favor of this new one): https://developer.yahoo.com/weather/documentation.html#commercial
Their only examples are in PHP and Java. I've done my best to convert the Java example to .NET but I still am getting a "401 - Unauthorized" response. I've gone over my code several times and cannot find any problems so I'm hoping someone else will be able to see where I went wrong. Code is below.
Private Sub _WeatherLoader_DoWork(sender As Object, e As DoWorkEventArgs)
Try
Dim oauth As New OAuth.OAuthBase
Dim forecastRssResponse As query
Dim appId As String = My.Settings.YahooAppID
Dim consumerKey As String = My.Settings.YahooAPIConsumerKey
Dim yahooUri As String = String.Format("{0}?location=billings,mt&format=xml", YAHOO_WEATHER_API_BASE_ENDPOINT)
Dim oAuthTimestamp As Integer = oauth.GenerateTimeStamp()
Dim oAuthNonce As String = oauth.GenerateNonce()
Dim parameters As New List(Of String)
Try
parameters.Add(String.Format("oauth_consumer_key={0}", consumerKey))
parameters.Add(String.Format("oauth_nonce={0}", oAuthNonce))
parameters.Add("oauth_signature_method=HMAC-SHA1")
parameters.Add(String.Format("oauth_timestamp={0}", oAuthTimestamp.ToString()))
parameters.Add("oauth_version=1.0")
' Encode the location
parameters.Add(String.Format("location={0}", HttpUtility.UrlEncode("billings,mt", Encoding.UTF8)))
parameters.Add("format=xml")
' Sort parameters ascending
parameters = parameters.OrderBy(Function(item) item).ToList()
Dim i As Integer = 0
Dim builder As New StringBuilder()
Do While (i < parameters.Count())
builder.Append(String.Format("{0}{1}", If(i > 0, "&", String.Empty), parameters(i)))
i += 1
Loop
Dim signatureString As String = String.Format("GET&{0}&{1}", HttpUtility.UrlEncode(YAHOO_WEATHER_API_BASE_ENDPOINT, Encoding.UTF8), HttpUtility.UrlEncode(builder.ToString(), Encoding.UTF8))
Dim oAuthSignature As String = _CreateOauthSignature(signatureString)
Dim authorizationLine As String = String.Format("OAuth oauth_consumer_key={0}, oauth_nonce={1}, oauth_timestamp={2}, oauth_signature_method=HMAC-SHA1, oauth_signature={3}, oauth_version=1.0", consumerKey, oAuthNonce, oAuthTimestamp, oAuthSignature)
Dim forecastRequest As WebRequest = WebRequest.Create(yahooUri)
forecastRequest.Headers.Add("Authorization", authorizationLine)
forecastRequest.Headers.Add("Yahoo-App-Id", appId)
' Cast to HttpWebRequest to set ContentType through property
CType(forecastRequest, HttpWebRequest).ContentType = "text/xml"
Dim forecastResponse As WebResponse = forecastRequest.GetResponse()
If forecastResponse IsNot Nothing Then
Using responseStream As Stream = forecastResponse.GetResponseStream()
Dim rssDoc As New XmlDocument()
rssDoc.Load(responseStream)
forecastRssResponse = rssDoc.OuterXml().FromXml(Of query)()
End Using
e.Result = forecastRssResponse
End If
Catch ex As Exception
e.Result = Nothing
LoadingManually = False
End Try
Catch ex As Exception
modMain.SendDevErrorEmail(ex, "_WeatherLoader_DoWork in WeatherWidget", "Catch around dowork code in fired from refresh timer event in wether widget")
e.Result = Nothing
LoadingManually = False
End Try
End Sub
Private Function _CreateOauthSignature(baseInfo As String) As String
Dim secretKey As String = String.Format("{0}&", My.Settings.YahooAPIConsumerSecretKey)
Dim encoding As New System.Text.ASCIIEncoding()
Dim keyBytes As Byte() = encoding.GetBytes(secretKey)
Dim messageBytes As Byte() = encoding.GetBytes(baseInfo)
Dim hashMessage As Byte()
Using hmac As New HMACSHA1(keyBytes)
hashMessage = hmac.ComputeHash(messageBytes)
End Using
Return Convert.ToBase64String(hashMessage)
End Function
After painstakingly creating a Java app, pasting in the Java example and stepping through it I found that the issue is in a poorly implemented URL Decode function on the receiving end.
In the Java app, URL Encode uses upper case characters while in .NET HTTPUtility.URLEncode uses lower case characters. This is enough to throw off your signature and cause a 401 - Unauthorized error.
My solution was to create a string extension method that will URL Encode in upper case:
<Extension>
Public Function UppercaseURLEncode(ByVal sourceString As String) As String
Dim temp As Char() = HttpUtility.UrlEncode(sourceString).ToCharArray()
For i As Integer = 0 To temp.Length - 2
If temp(i).ToString().Equals("%", StringComparison.OrdinalIgnoreCase) Then
temp(i + 1) = Char.ToUpper(temp(i + 1))
temp(i + 2) = Char.ToUpper(temp(i + 2))
End If
Next
Return New String(temp)
End Function
Using this extension method my signature gets created exactly like the one in the Java app and I am able to retrieve the response.
Hope this helps other .net programmers with this issue!

how to hide other column

In my datagridview, I just want to show other fields such as ID,LastName,FirstName, and MiddleName and i dont want to show any fields but i want it to retrieve even it's hidden. But when i specify what i just want to show in my datagridview it causes runtime error.
this is my code to load the datagridview.
MysqlConn.Open()
Dim Query As String
Query = "select ID,LastName,FirstName,MiddleName from god.precord"
COMMAND = New MySqlCommand(Query, MysqlConn)
SDA.SelectCommand = COMMAND
SDA.Fill(dbDataSet)
bSource.DataSource = dbDataSet
DataGridView1.DataSource = bSource
SDA.Update(dbDataSet)
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
then this is my code for retrieve data into textboxes
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
txtid.Text = row.Cells("ID").Value.ToString
txtlastname.Text = row.Cells("LastName").Value.ToString
txtfirstname.Text = row.Cells("FirstName").Value.ToString
txtmiddlename.Text = row.Cells("MiddleName").Value.ToString
txtaddress.Text = row.Cells("Address").Value.ToString
txtcontactno.Text = row.Cells("ContactNo").Value.ToString
txtgender.Text = row.Cells("Gender").Value.ToString
dtpbirthdate.Text = row.Cells("Birthdate").Value.ToString
txtage.Text = row.Cells("Age").Value.ToString
End If
End Sub
runtime error
please help me this is for my thesis
thankyou in advance <3
You have to add hidden column into the select query to retreive the data.
Hide the column from the DataGridView instead.
try
MysqlConn.Open()
Dim Query As String
Query = "select ID,LastName,FirstName,MiddleName,Address from god.precord"
COMMAND = New MySqlCommand(Query, MysqlConn)
SDA.SelectCommand = COMMAND
SDA.Fill(dbDataSet)
bSource.DataSource = dbDataSet
DataGridView1.DataSource = bSource
SDA.Update(dbDataSet)
DataGridView1.Columns("Address").Visible = false
MysqlConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try

Mail merge with Libre Office using .net

I have following code block which is working perfectly for OpenOffice SDK to automate Mail merge functionality.
Public Function runQueryOnDataSource(ByVal nameOfdDtaource As String, ByVal query As String) As Boolean
strLog = strLog + vbCrLf + Now.ToString() + ": runQueryOnDataSource nameOfdDtaource:" + nameOfdDtaource + ",query:" + query + "-Started"
Dim oDB As Object, oBase As Object
Dim oStatement As Object
Dim rSQL As String
Dim oRequete As Object
Dim oServiceManager As Object, CreateUnoService As Object
Try
'Creation instance Open office
oServiceManager = CreateObject("com.sun.star.ServiceManager")
CreateUnoService = oServiceManager.createInstance("com.sun.star.sdb.DatabaseContext")
mxMSFactory = (uno.util.Bootstrap.bootstrap()).getServiceManager()
oDB = CreateUnoService.getByName(nameOfdDtaource) 'oDB=XDataSource
'Connection
oBase = oDB.getConnection("", "") 'oBase=XConnection
oStatement = oBase.createStatement 'XStatement
'rSQL = "SELECT * FROM ""26_MailMergeResult_DEMO"
rSQL = query
oRequete = oStatement.execute(rSQL)
Return True
Catch ex As Exception
strLog = strLog + vbCrLf + Now.ToString() + ": Exception" + ex.ToString()
Throw ex
Finally
oDB = Nothing
oBase.Close()
oBase.Dispose()
End Try
strLog = strLog + vbCrLf + Now.ToString() + ": runQueryOnDataSource-Finished"
Return True
End Function
Above code is used to insert data into the DataSource already registered with libre office.But Now when I try to use it, line oServiceManager = CreateObject("com.sun.star.ServiceManager") generates error "Error Creating ActiveX object".
Do anyone have idea, How do I fix this.
This code does not look right, so I'm surprised it ever worked. In other examples, the bootstrap() line always goes first. Then use that service manager instead of a separate oServiceManager variable.
For example, see the Java code at https://www.openoffice.org/udk/common/man/spec/transparentofficecomponents.html.
EDIT:
You're almost there. The getByName() method returns uno.Any, which has a property called Value that DirectCast can use.
Dim oDB As XDataSource
Dim oBase As XConnection = Nothing
Dim xContext As XComponentContext = uno.util.Bootstrap.bootstrap()
Dim xMSFactory As XMultiServiceFactory = DirectCast(
xContext.getServiceManager(), XMultiServiceFactory)
Dim xNameAccess As XNameAccess = DirectCast(
xMSFactory.createInstance("com.sun.star.sdb.DatabaseContext"), XNameAccess)
oDB = DirectCast(xNameAccess.getByName("Bibliography").Value, XDataSource)
oBase = DirectCast(oDB.getConnection("", ""), XConnection)

How to copy data from a form+sub-form to table in ms access?

I have created a form that has a subform attached. I have a button that runs a query to delete the record but first I want to copy the data from including all subform information if available to a table. I have used the following code but nothing happens. please! what am I have missing?
Private Sub Command63_Click()
Dim db As Database, delfile As Recordset, Criteria As String
Set db = CurrentDb
Set delfile = db.OpenRecordset("DelFile", DB_OPEN_DYNASET)
'add data to deleted taxpayer file table
With delfile
.AddNew
!DeletedBy = (Forms!MainMenu!username)
!Branch = Me.Branch
!TaxType = Me.TaxType
!Volume = Me.Volume
!Keyedby = Me.Keyedby
!DateKeyed = Me.DateKeyed
!CreatedAt = Me.CreatedAt
!Comment = Me.Comment
End With
delfile.Close
db.Close
End Sub
Once you have set all of your field values you need to include a .update for the changes to take affect. Your new code would look like this.
Private Sub Command63_Click()
Dim db As Database, delfile As Recordset, Criteria As String
Set db = CurrentDb
Set delfile = db.OpenRecordset("DelFile", DB_OPEN_DYNASET)
'add data to deleted taxpayer file table
With delfile
.AddNew
!DeletedBy = (Forms!MainMenu!username)
!Branch = Me.Branch
!TaxType = Me.TaxType
!Volume = Me.Volume
!Keyedby = Me.Keyedby
!DateKeyed = Me.DateKeyed
!CreatedAt = Me.CreatedAt
!Comment = Me.Comment
.Update
End With
delfile.Close
db.Close
End Sub

How to learn ADO.NET

I need to learn ADO.NET to build applications based on MS Office. I have read a good deal about ADO.NET in the MSDN Library, but everything seems rather messy to me.
What are the basics one must figure out when using ADO.NET? I think a few key words will suffice to let me organize my learning.
There are three key components (assuming ur using SQL server):
SQLConnection
SqlCommand
SqlDataReader
(if you're using something else, replace Sql with "Something", like MySqlConnection, OracleCommand)
Everything else is just built on top of that.
Example 1:
using (SqlConnection connection = new SqlConnection("CONNECTION STRING"))
using (SqlCommand command = new SqlCommand())
{
command.commandText = "SELECT Name FROM Users WHERE Status = #OnlineStatus";
command.Connection = connection;
command.Parameters.Add("#OnlineStatus", SqlDbType.Int).Value = 1; //replace with enum
connection.Open();
using (SqlDataReader dr = command.ExecuteReader))
{
List<string> onlineUsers = new List<string>();
while (dr.Read())
{
onlineUsers.Add(dr.GetString(0));
}
}
}
Example 2:
using (SqlConnection connection = new SqlConnection("CONNECTION STRING"))
using (SqlCommand command = new SqlCommand())
{
command.commandText = "DELETE FROM Users where Email = #Email";
command.Connection = connection;
command.Parameters.Add("#Email", SqlDbType.VarChar, 100).Value = "user#host.com";
connection.Open();
command.ExecuteNonQuery();
}
Another way of getting a command object is to call connection.CreateCommand().
That way you shouldn't have to set the Connection property on the command object.