Trouble writing a select query [closed] - sql-server

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to select table data in my page. Kindly provide a solution for my select query.
This is my code:
con.Open()
cmd = New SqlCommand("select name from wq where id='" + TextBox1.Text + "'", con)
dr = cmd.ExecuteReader
While dr.Read
TextBox2.Text = dr(0)
End While
dr.Close()
con.Close()

It looks like vb code if you want c# code then use following code
cmd = new SqlCommand("select name from wq where id='" + TextBox1.Text + "'", con);
con.Open();
dr = cmd.ExecuteReader();
dr.Read();
TextBox2.Text = dr[0].ToString();
dr.Close();
con.Close();

Related

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')"

Filtered drop down list SQL Server & VB.NET [closed]

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 4 years ago.
Improve this question
I'm not a programmer just a hobbyist trying to make a small app for myself.
My query is that I have two drop down lists (vb.net form), one is "builder" the next is "projects" (both linked via "BuilderID" in MSSQL) and i want to filter the "Projects" List to the ones linked to the selected "Builder"
My "builder" drop down list is fine, I was thinking of a stored procedure for the second "project List", some way of querying the project table based on the selection of builder.
im trying to edit this question but not sure what to add as the question was answered fairly well by mary who must have understood what i was asking.
I was not sure how you were filling your first list so I started from the beginning. The important thing is to set the ValueMember to the ID. Then using the SelectedIndexChanged event you can get the builder ID for your next query. Same theory applies to filling the grid. Very adorable baby!
Imports System.Data.SqlClient
Public Class DWGBrowse
Public Property FinishedLoading As Boolean = False
Private Sub DWGBrowse_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FillBuildersList()
FinishedLoading = True
End Sub
Private Sub FillBuildersList()
Dim strSQL = "Select * From Builders;"
Dim dt As New DataTable
Using cn As New SqlConnection(My.Settings.BuildersConnection)
Using cmd As New SqlCommand(strSQL, cn)
Try
cn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader
dt.Load(dr)
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
End Using
End Using
BuilderBox.DataSource = dt
BuilderBox.DisplayMember = "BuilderName" 'Whatever your column is called
BuilderBox.ValueMember = "BuilderId" 'the name of your ID column
BuilderBox.SelectedIndex = -1
End Sub
Private Sub BuilderBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles BuilderBox.SelectedIndexChanged
If Not FinishedLoading Then Exit Sub
Try
Using sqlConnection As New System.Data.SqlClient.SqlConnection(My.Settings.BuildersConnection)
Dim id As Integer = CInt(BuilderBox.SelectedValue)
Dim dt As New DataTable
Dim sqlString As String = "Select * From Projects Where BuilderID = #ID"
Using cmd As New System.Data.SqlClient.SqlCommand(sqlString, sqlConnection)
cmd.Parameters.Add("#ID", SqlDbType.Int).Value = id
ProjectBox.DataSource = Nothing
sqlConnection.Open()
Using dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader
dt.Load(dr)
ProjectBox.DisplayMember = "ProjectName"
ProjectBox.ValueMember = "ProjectID"
ProjectBox.DataSource = dt
End Using
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub ProjectBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ProjectBox.SelectedIndexChanged
Dim id As Integer = CInt(ProjectBox.SelectedValue)
Dim dt As New DataTable
Dim strSQL As String = "Select * From Drawings Where ProjectID = #ID"
Using cn As New SqlConnection(My.Settings.BuildersConnection)
Using cmd As New SqlCommand(strSQL, cn)
cmd.Parameters.Add("#ID", SqlDbType.Int).Value = CInt(ProjectBox.SelectedValue)
cn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader
dt.Load(dr)
End Using
End Using
End Using
DataGridView1.DataSource = dt
End Sub
End Class

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

incorrect syntax near keyword where [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
SqlQuery = "INSERT INTO GradeTable(Grade,UserID,RegDate)Values('" & TxtGrade.Text.Trim & "','" & UserID & "','" & DTBReg.Value & "'Where GradeTable.StudentID ='" & StudentID & "' and GradeTable.CourseID= '" & CourseID & "') "
It seems for what you are trying to do, that what you need is an UPDATE statement and not an INSERT as you are writing. This seems a much more appropiate query:
UPDATE GradeTable
SET Grade = #grade,
UserID = #UserID,
RegDate = #RegDate
WHERE StudentID = #StudentID AND CourseID = #CourseID
Note that I used parameters and not the value of your textbox. This is because you are being vulnerable to one of the most common database security flaws that is SQL Injection.
Use that text for your query, and then you can add your values to the query this way:
cmd.Parameters.AddWithValue("#grade", TxtGrade.Text.Trim);
cmd.Parameters.AddWithValue("#UserID", UserID);
//AND SO ON WITH OTHER PARAMETERS
Assumming cmd is your SqlCommand

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