MongoDB Query in VB.Net - vb.net

I can not get VB.net connect to a mongodb replicaset - it throws exceptions
dim mongo as MongoDBClient
dim mgDB as MongoDatabase
dim mgcol as MongoCollection(of Bob)
public class Bob
public property ID as ObjectID
public property Name as string
end class
sub Main()
try
mongo = New MongoClient("mongodb://ip:port,ip:port,ip:port/?replicaSet=Bob")
mgDB = mongo.GetDatabase("test")
mscol = mgDB.Collection(of Bob)("test.me")
mgDB.GetCollection("test.me").Find(MongoDB.Driver.Builders.Query.Exists("id"))
catch ex as Exception
console.writeline(ex)
end try
end sub
I don't know how I'm supposed to query via VB .Net?

I got this to work
Dim _client As IMongoClient
Dim _db As IMongoDatabase
Sub Main()
System.Console.WriteLine("Hi, Bob!!")
Try
_client = New MongoClient("mongodb://mongoone:27020,mongotwo:27021,mongothree:27020/?replicaSet=Bob")
_db = _client.GetDatabase("Test")
Dim mgCol = _db.GetCollection(Of Bob)("test.me")
Dim mybob As Bob
mybob = New Bob
mybob.Name = "Sam6"
mybob.ID = ObjectId.GenerateNewId
'mgCol.InsertOne(mybob)
getSomedata().Wait()
'getDataByFilterExtists().Wait()
'getMetaCardSpectrum().Wait()
getMetaCardSpectrumObj().Wait()
Catch ex As Exception
Console.WriteLine(ex)
End Try
End Sub

Related

How to Change database name in Linq Vb.net

I have a database Linq dbml
I need to change the database name before creating
Dim dt As New DataSchool()
dt.Connection.Open()
dt.CreateDatabase()
Use This Method
Sub CreatesqldatabaseMSQL()
Try
Using connect As New DataSchool(GetSQLConnectionString(True))
connect.CreateDatabase()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
GetSQLConnectionString This Function Return SQLconnection string
Public Function GetSQLConnectionString(ByVal includeDatabase As Boolean) As String
Dim builder As New SqlConnectionStringBuilder()
'Build a connection string from the user input.'
builder.DataSource = Msql.datasource
builder.IntegratedSecurity = Msql.IntSec
If builder.IntegratedSecurity = False Then
builder.UserID = Msql.username
builder.Password = Msql.password
End If
If includeDatabase Then
builder.InitialCatalog = Msql.DatabaseName
End If
Return builder.ConnectionString
End Function
Msql.Server, Msql.DatabaseName and etc this is Structure, You can change database name From here
Public Structure Msql
Shared datasource As String
Shared IntSec As Boolean
Shared username As String
Shared password As String
Shared DatabaseName As String
End Structure

Adding a dynamic member to an ExpandoObject

In the following method I am trying to add a dynamic member to an expando object but it throws an exception:
public member not found for expand object
Private Sub GetAckValues()
Try
Dim ack_duration As String = String.Empty
Dim ack_by_user_fkid As String = String.Empty
Dim ack_time As String = String.Empty
ack_duration = txtdefaultack.Text
ack_by_user_fkid = Convert.ToString(Session("user_code"))
Dim Ack_Detail As Object = New ExpandoObject()
Ack_Detail.ack_duration = ack_duration
Ack_Detail.ack_by_user_fkid = ack_by_user_fkid
receiptObject.StatusObject = Ack_Detail
Catch ex As Exception
logger.Error("Enter JobRequest form done by :" & LoggedinUserId, ex)
End Try
End Sub
Should this:
Ack_Detail.ack_duration = Ack_Detail.ack_duration
actually be this:
Ack_Detail.ack_duration = ack_duration

Serialize and Base64 convert and vice versa in vb.net

I have functions which serialize and base64 convert objects and vice versa. It uses Newtonsoft to serialize and deserialize Objects. The code works but is there a way to do this without using Newtonsoft?
Public Shared Function SerializeAndBase64(ObjectToSerialize As Object) As ReturnObject(Of String)
Dim rtnObj As New ReturnObject(Of String)
Try
Dim SerializedObjectJson As String = Newtonsoft.Json.JsonConvert.SerializeObject(ObjectToSerialize)
Dim Base64String As String = Convert.ToBase64String(Encoding.UTF8.GetBytes(SerializedObjectJson))
rtnObj.Item = Base64String
Catch ex As Exception
rtnObj.ErrorID = ErrorHandler.handleError(ex)
rtnObj.ErrorMessage = ex.Message
rtnObj.IsError = True
End Try
Return rtnObj
End Function
Public Shared Function DeserializeFromBase64(Of t)(Base64Json As String) As ReturnObject(Of t)
Dim rtnObj As New ReturnObject(Of t)
Try
Dim SerializedObjectJson As String = Encoding.UTF8.GetString(Convert.FromBase64String(Base64Json))
Dim DeserializedObject As t = Newtonsoft.Json.JsonConvert.DeserializeObject(Of t)(SerializedObjectJson)
rtnObj.Item = DeserializedObject
Catch ex As Exception
rtnObj.ErrorID = ErrorHandler.handleError(ex)
rtnObj.ErrorMessage = ex.Message
rtnObj.IsError = True
End Try
Return rtnObj
End Function
Thanks in advance.
You can Serialize/Deserialize it yourself. You just have to come up with a way to save everything into a single string, or XML or even a byte array if you want. Your choice.
Public Class Class1
Public Property MyValue As String
Public Function Serialize() As String
Return MyValue
End Function
Public Sub Deserialize(ByVal value As String)
MyValue = value
End Sub
End Class

Database fields dont update when click event is triggered

the code I have runs without any errors but the database doesnt update its fields.
This runs with no errors but database doesnt update its fields
Sub UpdatePassword(ci As ClientInformation)
Try
Using con As New SqlCeConnection(GetConString)
If con.State = Data.ConnectionState.Closed Then con.Open()
Dim adapt As New SqlCeDataAdapter(String.Format("UPDATE AdminInformation
where ClientID='{0}' SET PWD='{1}'", ci.ClientID, ci.Password), con)
End Using
Catch ex As Exception
End Try
End Sub
my ClientInformation Class:
Public Class ClientInformation
Public Property ClientID As String
Public Property SubscriptionEndDate As Date
Property FirstName As String
Property Surname As String
Property Email As String
Property Company As String
Property PostalAdd As String
Property Country As String
Property Tel As String
Property Fax As String
Property Password As String
End Class
my event handler:
Protected Sub btnChangePass_Click(sender As Object, e As EventArgs) Handles
btnChangePass.Click
Dim ci As New ClientInformation
If txtBoxNewPass.Text = txtBoxConfirmNewPass.Text Then
ci.Password = txtBoxNewPass.Text
lblConfirmPasswordMsg.Visible = False
dh.UpdatePassword(ci)
lblPasswordChanged.Visible = True
txtBoxNewPass.Text = ""
txtBoxConfirmNewPass.Text = ""
Else
txtBoxNewPass.Text = ""
txtBoxConfirmNewPass.Text = ""
lblConfirmPasswordMsg.Visible = True
End If
End Sub
my connection string:
Private Function GetConString() As String
Return ConfigurationManager.ConnectionStrings("conStringLicenses").ConnectionString
End Function
any help will be appreciated, thanks in advance.
PS database is sql compact, visual studio 2012, windows 7
I think your query should be like this
UPDATE AdminInformation SET PWD='{1}' where ClientID='{0}'
You started your WHERE clause before your SET
UPDATE:
Try changing
Dim adapt As New SqlCeDataAdapter(String.Format("UPDATE AdminInformation where ClientID='{0}' SET PWD='{1}'", ci.ClientID, ci.Password), con)
into this
Dim cmd As New SqlCeCommand(String.Format("UPDATE AdminInformation SET PWD='{1}' where ClientID='{0}'", ci.ClientID, ci.Password), con)
cmd.ExecuteNonQuery()

vb.net return json object with multiple types?

I need to return some data from a web service that looks something like this:
data.page = 1
data.count = 12883
data.rows(0).id = 1
data.rows(0).name = "bob"
data.rows(1).id = 2
data.rows(1).name = "steve"
data.rows(2).id = 3
data.rows(2).name = "fred"
I have no idea how to do this. I've returend simple types and simple arrays, but never an object like this.
The data source is a sql Database. The target is a javascript/ajax function. I'm currently successfully returning the rows themselves as a dataset and it works, but I need to add the count and a couple other "parent level" variables.
For the sake of full disclosure, here is the code that is working:
<WebMethod()> _
Public Function rptPendingServerRequests() As DataSet
Dim connetionString As String
Dim connection As SqlConnection
Dim command As SqlCommand
Dim adapter As New SqlDataAdapter
Dim ds As New DataSet
Dim sql As String
connetionString = "..."
sql = "SELECT usm_request.request_id, usm_request.status, usm_request.req_by_user_id " +
"FROM usm_request " +
"WHERE usm_request.request_id in " +
"(SELECT distinct(usm_request.request_id) from usm_request, usm_subscription_detail WHERE usm_request.request_id = usm_subscription_detail.request_id " +
"AND usm_subscription_detail.offering_id = 10307) ORDER BY usm_request.request_id DESC"
connection = New SqlConnection(connetionString)
Try
connection.Open()
command = New SqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds)
adapter.Dispose()
command.Dispose()
connection.Close()
Return ds
Catch ex As Exception
End Try
End Function
And I'm trying to consume it with FlexiGrid. I've been working at it for a few hours with no luck. I basically need to convert the PHP at the following site to .net
http://code.google.com/p/flexigrid/wiki/TutorialPropertiesAndDocumentation
I think that you would be much better off just creating a couple of classes and moving the data from the database into these classes. For example:
Public Class MyDataClass
Public Property Page As Integer
Public ReadOnly Property Count As Integer
Get
If Me.Rows IsNot Nothing Then
Return Me.Rows.Count
Else
Return 0
End If
End Get
End Property
Public Property Rows As List(Of MyDataRow)
' Parameterless constructor to support serialization.
Public Sub New()
Me.Rows = New List(Of MyDataRow)
End Sub
Public Sub New(wPage As Integer, ds As DataSet)
Me.New()
Me.Page = wPage
For Each oRow As DataRow In ds.Tables(0).Rows
Dim oMyRow As New MyDataRow
oMyRow.Id = oRow("id")
oMyRow.Name = oRow("Name")
Me.Rows.Add(oMyRow)
Next
End Sub
End Class
Public Class MyDataRow
Public Property Id As Integer
Public Property Name As String
' Parameterless constructor to support serialization
Public Sub New()
End Sub
End Class
Then change the return type of the method to MyDataClass and change the return to:
Return New MyDataClass(1, ds)