C# to VB.net conversion [closed] - c#-to-vb.net

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Could you please convert this two lines of code to VB.net?
MembershipUser newUser = Membership.GetUser(reg.UserName);
Guid newUserID=(Guid)newUser.ProviderUserKey;

http://www.developerfusion.com/tools/convert/csharp-to-vb/ outputs:
'reg is CreateUserWizard
Dim newUser As MembershipUser = Membership.GetUser(reg.UserName)
Dim newUserID As Guid = DirectCast(newUser.ProviderUserKey, Guid)

Try this:
'reg is CreateUserWizard
Dim newUser As MembershipUser = Membership.GetUser(reg.UserName)
Dim newUserID As Guid = DirectCast(newUser.ProviderUserKey, Guid)

Related

Data type conversion error between SQL Server int and MS Access VBA long data types [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Using an ms access 2007 form I am trying to manipulate a particular record from a linked SQL Server 2005 table.
'Form code
Private Sub orderid_DblClick(Cancel As Integer)
Dim idv As Long
idv = Me.orderid.Value
'...
Call manipulateRecord(idv)
end sub
'module code
Public Sub manipulateRecord(pidp As Long)
Dim rs0 As Recordset
Set rs0 = CurrentDb.OpenRecordset(dbSeeChanges, "select * from tableorders where idorder=" & pidp)
'this line produces the error "Run-time error '3421' data type conversion error"
'replacing the query with "select idorder from tableorders where idorder=" & pidp , produces the same error so that there is a mismatch between sqlserver int and vba long
'Setting pidp as integer produces overflow error instead
'...
end sub
tableorders is a linked sqlserver 2005 table
idorder field is of type int
How can get recognized the SQL Server 2005 int in MA Access 2007 VBA?
This has nothing to do with long and int - your parameters are in the wrong order.
CurrentDb.OpenRecordset(dbSeeChanges, "select * from tableorders where idorder=" & pidp)
must be
CurrentDb.OpenRecordset("select * from tableorders where idorder=" & pidp, , dbSeeChanges)

SqlCommand that inserts data has no effect [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to insert data into an existing Microsoft SQL Server 2014 table using VB.NET 2017. The code does not give me any errors, but it does not insert the record either. Right now I just want to get it to where it alters data, without worrying about user input. Any help is appreciated.
My guess is that it is not connecting to SQL Server properly.
Imports System.Data.SqlClient
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlstr As String
Dim connectionString As String = "server=localhost;database= database1;Trusted_Connection=True;"
sqlstr = "Insert into tblname (id, gender) VALUES ([5], ['Bob'])"
Try
Using connection As New SqlConnection(connectionString)
connection.Open()
Dim cmdInsert As New SqlCommand(sqlstr, connection)
connection.Close()
End Using
MsgBox("anything") 'this is just to see if it even runs
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
After you create the SqlCommand, just call ExecuteNonQuery to run the SQL statement:
Dim cmdInsert As New SqlCommand(sqlstr, connection)
cmdInsert.ExecuteNonQuery()
connection.Close()
But note that the values in your INSERT statement should not be surrounded by brackets:
sqlstr = "Insert into tblname (id, gender) VALUES (5, 'Bob')"

Hashing Passwords for login form in SQL Server VB.NET [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have a login form in VB.NET that requires a username and password input. If the user inputs match the correct login details in the SQL Server DB then another VB form is shown.
The password needs to be hashed, without using MD5.
Dim Bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(txtPassword.Text)
Dim HashofBytes() As Byte = New System.Security.Cryptography.SHA1Managed().ComputeHash(bytes)
Dim StrHash As String = Convert.ToBase64String(HashofBytes)
Using con As New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=""G:\Program\X\Database1.mdf"";Integrated Security=True")
con.Open()
Dim query As String = "SELECT COUNT(*) FROM Users WHERE Username=#Username AND Password=#Password"
Dim cmd As New SqlCommand(query, con)
cmd.Parameters.Add(New SqlParameter("#Username", txtUsername.Text))
cmd.Parameters.Add(New SqlParameter("#Password", StrHash))
Try
If cmd.ExecuteScalar() = 1 Then
frmOverview.ShowDialog()
Me.Hide()
Else
MsgBox("You have entered an invalid username or password")
End If
Catch ex As SqlException
MsgBox(ex.Message.ToString())
End Try
End Using
txtPassword.Clear()
However, the issue I have is that even if the user inputs the correct login details the next form is not shown. How could this be resolved?
As #Plutonix said, check your Password where clause:
SELECT COUNT(*) FROM Users WHERE Username=#Username AND #Password=#Password
it should be:
...AND Password=#Password

Writing a SQL statement while declaring variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to write a SQL statement in a VB application. In this case, I need to declare two variables to call other stored procedures. This code works when I run it in SSMS, but I am not sure how to do it in VB.
Statement:
DECLARE #NewIncidentNoteId NVARCHAR(15)
EXEC uspGetNextId #NewIncidentNoteId OUTPUT
INSERT INTO [dbo].[IncidentNotes]
([Id]
,[IncidentId]
,[TaskId]
,[VLevel]
,[NotesSourceType]
,[Notes]
,[LogDate]
,[LogTime]
,[LogTZ]
,[LogBy]
,[LogByName]
,[NotifyFlg]
,[ActivityDate]
,[ActivityTime]
,[ShowInReport])
VALUES()
(#NewIncidentNoteId
,'60Z3M'
,''
,'1'
,'EIRInvestigatorComment'
,'This is a TEST note.'
,'20150416'
,'10:10:10'
,'UTC'
,'12876'
,'USER, ADMIN'
,'False'
,'20150416'
,'12:13:13'
,'1'))
First of all, as others have said, the best option would be to actually add it to the database as a stored procedure. Then you could just execute the stored procedure via a simple ADO.NET command. However, if you insist on sending the entire SQL script as a command, there are several ways to get it as a string.
VB doesn't currently have great support for multi-line string literals (long-overdue support for that is being added in VB.NET 14 as part of Visual Studio 2015). There are several workarounds. See this other answer of mine for some good options. However, for something like this, you may want to look into reading the script from an embedded resource or an external script file.
Set cmd = New ADODB.Command
cmd.ActiveConnection = con
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "empdetails"
cmd.Parameters.Append cmd.CreateParameter_
("empid", adVarChar, adParamInput, 6, str_empid)
Set rs = cmd.Execute
If Not rs.EOF Then
txt_firstname = rs.Fields(0)
txt_title = rs.Fields(1)
txt_address = rs.Fields(2)
End If
Set cmd.ActiveConnection = Nothing
this code show how to use SP in vb6 not sure which vb you use if it is VB.net then you can use ADO.net to do that
this link might be helpful too
http://www.codeproject.com/Articles/15222/How-to-Use-Stored-Procedures-in-VB

Visual Basic problems when connection to SQL Server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Me and my 'team' from our informatics class have been trying to get a program running and connecting it to a SQL server, it seems we keep getting errors. Could you help me try and figure out the problem, I have been trying to fix this for 3 straight days so any help is welcome.
Code we have so far:
Try
Dim conn As New MySqlConnection
Dim test As String = 64012478
conn.ConnectionString = "User ID=*****_****; Password=******; Initial Catalog=dainformat_idea; Data Source=(IP of website);"
conn.Open()
Dim query = "SELECT * From students"
Dim commando As New MySqlCommand(query, conn)
Dim reader As MySqlDataReader = commando.ExecuteReader
reader.Read()
If reader.HasRows Then
MessageBox.Show(reader(0) & " " & reader(1) & "Kaas")
Exit Sub
Else
MessageBox.Show("Could not find something")
Exit Sub
End If
conn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try`
You are trying to connect a SQL Server with the MySQL libraries... If you have mySQL on the server the code will run fine, but only if you have mysql, otherwise the connection will fail.

Resources