This is a tennis tournament simulation program I'm Trying to make a tree. Everything its working perfectly but I have a big problem. I can't progress to the next stage with the winners of the firts games. Because I can't change the "target" label where I want to write the winners name. Here's a print:
This is how I Generate the winners who is going to the next stage and it actually works.~
Public Sub Gerar_Vencedor(Atleta1, Atleta2)
Using con As New OleDb.OleDbConnection
con.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0;" &
"Data Source = E:\dev\Ganso\BaseDados_ClubeTenis.accdb"
con.Close()
con.Open()
Dim busca = "Select Vencedor From Jogo Where idAtleta1 = '" & Atleta1 & "' and idAtleta2 = '" & Atleta2 & "'"
Dim cmd As OleDbCommand = New OleDbCommand(busca, con)
Dim dr As OleDbDataReader = cmd.ExecuteReader()
' A variável sql vai receber a string para fazer o select a base de dados
'Try
' é aberta ligação a Bdados
' declaração de um comando que vai executar a instrução sql na base de dados
' variável que vai receber os registos resultantes da instrução sql
' ciclo que vai percorrer todos os registos do comando anterior
While dr.Read()
Label9.Text = dr("Vencedor")
End While
con.Close()
End Using
End Sub
My problem is that Label9.Text = dr("Vencedor") because everytime I get a winner it goes to label9.
My final question is: Is there anyway to dynamically change the label without repeating this function over and over.
Thanks.
Can You Try
Dim i As Integer = 9
While dr.Read()
If i < 15
DirectCast(Me.Controls("Label" & i), Label).Text = dr("Vencedor")
Else
End If
i = i + 1
End While
can you please help me in VB.NET here is a picture of the database..
im getting crazy i can't find the error in the sentence
here goes the code
oConn.ConnectionString = strConn
oConn.Open()
If oConn.State = ConnectionState.Open Then
ACommand = New OleDbCommand("SELECT reparacoes.acessorios,maquinas.marca FROM reparacoes INNER JOIN maquinas on reparacoes.cod_maquina=maquinas.cod_maquina", oConn)
'define um dataAdapter
AAdapter = New OleDbDataAdapter()
AAdapter.SelectCommand = ACommand
'define e preenche um DataTable com os dados
ATabela = New DataTable()
AAdapter.Fill(ATabela)
' associar campos a base de dados
xy = ATabela.CreateDataReader
' Ler da tabela
linha = ACommand.ExecuteReader
While linha.Read()
End While
End If
oConn.Close()
I can't find the error,
thanks for all your help
Best Regards
José Lopes
I am building an e-commerce website as a school project. How can I make sure that I won't have duplicate users since this is the page where they have to fill in their user details.
This is the code I am using right now and the ExecuteNonquery doesn't work. It doesn't give any errors, it just skips it.
Protected Sub bntVerzenden_Click(sender As Object, e As EventArgs) Handles bntVerzenden.Click
Dim sqlCommand As String
Dim intGebruikersID, intRubriekID As Integer
Dim VerkopenConn As New OleDb.OleDbConnection
Dim VerkopenCommArtikel, VerkopenCommGebruiker, GebruikersIDComm, RubriekIDComm, EmailComm As New OleDb.OleDbCommand
VerkopenConn.ConnectionString = My.Settings.SvShopConnection
VerkopenCommArtikel.Connection = VerkopenConn
VerkopenCommGebruiker.Connection = VerkopenConn
GebruikersIDComm.Connection = VerkopenConn
RubriekIDComm.Connection = VerkopenConn
EmailComm.Connection = VerkopenConn
'Email Controleren
sqlCommand = "SELECT * FROM tblGebruiker WHERE GebruikersEmail = '" & txtEmail.Text & "'"
EmailComm.CommandText = sqlCommand
If VerkopenConn.State = ConnectionState.Closed Then VerkopenConn.Open()
If EmailComm.ExecuteNonQuery = 0 Then
'Gebruiker gegevens aan de database toevoegen
VerkopenCommGebruiker.CommandText = "INSERT INTO tblGebruiker (GebruikersNaam, GebruikersVoornaam, GebruikersEmail, GebruikersGSM) VALUES (#GebruikersNaam, #GebruikersVoornaam, #GebruikersEmail, #GebruikersGSM)"
VerkopenCommGebruiker.Parameters.AddWithValue("#GebruikersNaam", txtNaam.Text)
VerkopenCommGebruiker.Parameters.AddWithValue("#GebruikersVoornaam", txtVNaam.Text)
VerkopenCommGebruiker.Parameters.AddWithValue("#GebruikersEmail", txtEmail.Text)
VerkopenCommGebruiker.Parameters.AddWithValue("#GebruikersGSM", txtGsm.Text)
VerkopenCommGebruiker.ExecuteNonQuery()
'GebruikersID uit de database halen
GebruikersIDComm.CommandText = "SELECT tblGebruiker.GebruikersID FROM tblGebruiker WHERE tblGebruiker.GebruikersNaam = #Naam AND tblGebruiker.GebruikersVoornaam = #VNaam;"
GebruikersIDComm.Parameters.AddWithValue("#Naam", txtNaam.Text)
GebruikersIDComm.Parameters.AddWithValue("#VNaam", txtVNaam.Text)
intGebruikersID = Integer.Parse(GebruikersIDComm.ExecuteScalar)
'RubriekID uit de database halen
RubriekIDComm.CommandText = "SELECT tblRubriek.RubriekID FROM tblRubriek WHERE tblRubriek.RubriekNaam = #RubriekNaam"
RubriekIDComm.Parameters.AddWithValue("#RubriekNaam", lstRubriek.SelectedItem.ToString)
intRubriekID = Integer.Parse(RubriekIDComm.ExecuteScalar)
'Artikel gegevens aan de database toevoegen
VerkopenCommArtikel.CommandText = "INSERT INTO tblArtikel (ArtikelBeschrijving, ArtikelPrijs, RubriekID, ArtikelAankoopdatum, ArtikelTekoopgezet, ArtikelBeschikbaar, GebruikersID) VALUES ( #ArtikelBeschrijving, #ArtikelPrijs, #RubriekID, #ArtikelAankoopdatum, #ArtikelTekoopgezet, #ArtikelBeschikbaar, #GebruikersID)"
VerkopenCommArtikel.Parameters.AddWithValue("#ArtikelBeschrijving", txtArtikelBeschrijving.Text)
VerkopenCommArtikel.Parameters.AddWithValue("#ArtikelPrijs", txtArtikelPrijs.Text)
VerkopenCommArtikel.Parameters.AddWithValue("#RubriekID", intRubriekID)
VerkopenCommArtikel.Parameters.AddWithValue("#ArtikelAankoopdatum", CalAankoop.SelectedDate)
VerkopenCommArtikel.Parameters.AddWithValue("#ArtikelTekoopgezet", calVerkoop.SelectedDate)
VerkopenCommArtikel.Parameters.AddWithValue("#ArtikelBeschikbaar", True)
VerkopenCommArtikel.Parameters.AddWithValue("#GebruikersID", intGebruikersID)
VerkopenCommArtikel.ExecuteNonQuery()
VerkopenConn.Close()
Else
'GebruikersID uit de database halen
GebruikersIDComm.CommandText = "SELECT tblGebruiker.GebruikersID FROM tblGebruiker WHERE tblGebruiker.GebruikersNaam = #Naam AND tblGebruiker.GebruikersVoornaam = #VNaam;"
GebruikersIDComm.Parameters.AddWithValue("#Naam", txtNaam.Text)
GebruikersIDComm.Parameters.AddWithValue("#VNaam", txtVNaam.Text)
intGebruikersID = Integer.Parse(GebruikersIDComm.ExecuteScalar)
'RubriekID uit de database halen
RubriekIDComm.CommandText = "SELECT tblRubriek.RubriekID FROM tblRubriek WHERE tblRubriek.RubriekNaam = #RubriekNaam"
RubriekIDComm.Parameters.AddWithValue("#RubriekNaam", lstRubriek.SelectedItem.ToString)
intRubriekID = Integer.Parse(RubriekIDComm.ExecuteScalar)
'Artikel gegevens aan de database toevoegen
VerkopenCommArtikel.CommandText = "INSERT INTO tblArtikel (ArtikelBeschrijving, ArtikelPrijs, RubriekID, ArtikelAankoopdatum, ArtikelTekoopgezet, ArtikelBeschikbaar, GebruikersID) VALUES ( #ArtikelBeschrijving, #ArtikelPrijs, #RubriekID, #ArtikelAankoopdatum, #ArtikelTekoopgezet, #ArtikelBeschikbaar, #GebruikersID)"
VerkopenCommArtikel.Parameters.AddWithValue("#ArtikelBeschrijving", txtArtikelBeschrijving.Text)
VerkopenCommArtikel.Parameters.AddWithValue("#ArtikelPrijs", txtArtikelPrijs.Text)
VerkopenCommArtikel.Parameters.AddWithValue("#RubriekID", intRubriekID)
VerkopenCommArtikel.Parameters.AddWithValue("#ArtikelAankoopdatum", CalAankoop.SelectedDate)
VerkopenCommArtikel.Parameters.AddWithValue("#ArtikelTekoopgezet", calVerkoop.SelectedDate)
VerkopenCommArtikel.Parameters.AddWithValue("#ArtikelBeschikbaar", True)
VerkopenCommArtikel.Parameters.AddWithValue("#GebruikersID", intGebruikersID)
VerkopenCommArtikel.ExecuteNonQuery()
VerkopenConn.Close()
End If
System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">alert(""Jouw artikel staat nu te koop"")</SCRIPT>")
End Sub
You should hace some id field, like email or full name. You could create a unique index on this columna and catch the exception on insert. Or fire a first query to verify if the user is already added.
You can check whether the emailid that user provide is already exist in your database or not.Try this code:-
SqlConnection Conn=new SqlConnection(your_connectionstring);
SqlCommand command = new SqlCommand("select * from tblGebruiker where GebruikersEmail='" + emailid + "'", Conn);
DataTable dtl = new DataTable();
SqlDataAdapter sqlDA = new SqlDataAdapter(command);
sqlDA.Fill(dtl);
if (dtl.Rows.Count > 0)
{
Response.Write("<script>alert('User already registered')</script>");
}
else
{
VerkopenCommGebruiker.CommandText = "INSERT INTO tblGebruiker (GebruikersNaam, GebruikersVoornaam, GebruikersEmail, GebruikersGSM) VALUES (#GebruikersNaam, #GebruikersVoornaam, #GebruikersEmail, #GebruikersGSM)"
VerkopenCommGebruiker.Parameters.AddWithValue("#GebruikersNaam", txtNaam.Text)
VerkopenCommGebruiker.Parameters.AddWithValue("#GebruikersVoornaam", txtVNaam.Text)
VerkopenCommGebruiker.Parameters.AddWithValue("#GebruikersEmail", txtEmail.Text)
VerkopenCommGebruiker.Parameters.AddWithValue("#GebruikersGSM", txtGsm.Text)
If VerkopenConn.State = ConnectionState.Closed Then VerkopenConn.Open()
VerkopenCommGebruiker.ExecuteNonQuery()
}
I am sorry for my bad english. I hope somebody could explain what the error The conversion from type MySqlDataReader to type Integer is invalid means.
I was sure that it was a simple 'Dim a Integer, but It is larger than that, or I am just been stupid.
My btnLogin_Click code:
Dim SQL As New MySQLHook
Dim loginResult As Integer
Dim User As TextBox = Me.Controls.Item("TextGebruikersNaam")
Dim Pass As TextBox = Me.Controls.Item("TextGebruikersPass")
If String.IsNullOrEmpty(User.Text) Or String.IsNullOrEmpty(Pass.Text) Then
MsgBox("Voer een gebruikersnaam en wachtwoord in om verder te gaan.", MsgBoxStyle.Exclamation, "Foutmelding")
User.Focus()
Else
loginResult = SQL.Results("SELECT * FROM tblgebruikers " & "WHERE GebruikersNaam='" & User.Text & "' AND " & "GebruikersPass='" & Pass.Text & "'")
If loginResult = 1 Then
' Login was good, todo: add code here
Else
MsgBox("De gebruikersnaam of wachtwoord is onjuist. Probeer het opnieuw of vraag een nieuw wachtwoord aan.", MsgBoxStyle.Exclamation, "Foutmelding")
User.Text = ""
Pass.Text = ""
User.Focus()
End If
End If
My Results code (SQL.Results is from As New MySQLHook)
Public Function Results(ByVal sql)
Try
Dim conn = Connect()
Dim myAdapter As New MySqlDataAdapter
Dim myCommand As New MySqlCommand()
myCommand.Connection = conn
myCommand.CommandText = sql
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader()
Return myData
Catch myerror As MySqlException
clsError.logMessage("De server retourneerde de volgende foutmelding: " & myerror.Message)
Return DBNull.Value
End Try
End Function
I olso tried using this function but it gave me the same error message:
Public Function num_results(ByVal sql)
Try
Dim conn = Connect()
Dim myAdapter As New MySqlDataAdapter
Dim myCommand As New MySqlCommand()
myCommand.Connection = conn
myCommand.CommandText = sql
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader()
Return myData
Catch myerror As MySqlException
clsError.logMessage("De server retourneerde de volgende foutmelding: " & myerror.Message)
Return DBNull.Value
End Try
End Function
Why does this conversion error pop-ups? I was sure that 1 or 0 is self-explaining.
Your problem is that you are creating a data reader and returning that. You need to read the integer from the myData variable before returning it.
So, instead of:
Return myData
You should be able to use:
Return myData.GetInt32(0)
The 0 in that statement identifies the column you want to read, and since you are returning a single column from your SQL query, it is in index 0.
Once you have fixed that, please google 'SQL injection' and 'parameterized SQL queries' before continuing. Having your app open to SQL injection is a serious risk.
Okay, I am building a VB.NET system but I'm having troubles in catching an SQL exception in some parts of my code. In short, I am using a SELECT sql query to retrieve a particular record and I want to determine whether a record exist in a MSAccess database so that I don't retrieve 0 rows. 0 rows will lead to an exception during printing to a TextField. The following is a sample code I'm working on:
If txSearch.Text = "" Then
MsgBox("Please type the user id or use the barcode reader to scan", MsgBoxStyle.OkOnly, "Search Field Empty")
End If
'ElseIf txSearch.Text != "" Then
If txSearch.Text <> "" Then
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim dt As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim de As OleDb.OleDbDataAdapter
Dim sql As String
Dim sql1 As String
Dim temp_num As Integer
Dim cmd As New OleDb.OleDbCommand
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = new.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
Dim search As String
search = txSearch.Text
MessageBox.Show("You are going to search for " + search + ". Click OK to continue.")
sql = "SELECT * FROM Student_Details WHERE Admin_No = '" & txSearch.Text & "'"
sql1 = "SELECT * FROM Laptop_Details WHERE Admin_No = '" & txSearch.Text & "'"
da = New OleDb.OleDbDataAdapter(sql, con)
de = New OleDb.OleDbDataAdapter(sql1, con)
'Dim check As Integer = sql.
'If check > 0 Then
'da.SelectCommand = cmd
ds = New DataSet("Student_Details")
da.Fill(ds, "Student_Details")
dt = New DataSet("Laptop_Details")
de.Fill(dt, "Laptop_Details")
'con.Close()
'If sql <> "" And sql1 <> "" Then
'If ds.Equals(1) And ds.Equals(1) Then
txAdminNO.Text = ds.Tables("Student_Details").Rows(0).Item(0)
txName.Text = ds.Tables("Student_Details").Rows(0).Item(1)
txProgramme.Text = ds.Tables("Student_Details").Rows(0).Item(2)
cmbSStatus.SelectedText = ds.Tables("Student_Details").Rows(0).Item(3)
txSerial.Text = dt.Tables("Laptop_Details").Rows(0).Item(1)
txModel.Text = dt.Tables("Laptop_Details").Rows(0).Item(2)
Dim com As New OleDb.OleDbCommand(sql, con)
Dim com1 As New OleDb.OleDbCommand(sql1, con)
Try
temp_num = com.ExecuteNonQuery
temp_num = com1.ExecuteNonQuery
Catch ex As IndexOutOfRangeException
Trace.WriteLine(ex.ToString)
End Try
con.Close()
'End If
'End If
End If
Try something like this:
if ds.Tables("Student_Details").Rows.Count > 0
txAdminNO.Text = ds.Tables("Student_Details").Rows(0).Item(0)
txName.Text = ds.Tables("Student_Details").Rows(0).Item(1)
txProgramme.Text = ds.Tables("Student_Details").Rows(0).Item(2)
cmbSStatus.SelectedText = ds.Tables("Student_Details").Rows(0).Item(3)
End if
if ds.Tables("Laptop_Details").Rows.Count > 0
txSerial.Text = dt.Tables("Laptop_Details").Rows(0).Item(1)
txModel.Text = dt.Tables("Laptop_Details").Rows(0).Item(2)
End if
To help you with your comment I would do it like that:
if ds.Tables("Student_Details").Rows.Count > 0 And ds.Tables("Laptop_Details").Rows.Count > 0
txAdminNO.Text = ds.Tables("Student_Details").Rows(0).Item(0)
txName.Text = ds.Tables("Student_Details").Rows(0).Item(1)
txProgramme.Text = ds.Tables("Student_Details").Rows(0).Item(2)
cmbSStatus.SelectedText = ds.Tables("Student_Details").Rows(0).Item(3)
txSerial.Text = dt.Tables("Laptop_Details").Rows(0).Item(1)
txModel.Text = dt.Tables("Laptop_Details").Rows(0).Item(2)
else
MsgBox("The user with id "+txSearch.Text+" does not exist")
end if
Try to use try-catch statement:
Try
Your code here for your query statements.
Catch ex as Exception MsgBox(ex.Message)End Try