|
Hello
I am programming with vb.net and mysql database, and I have a database 'boky' and 3 tables, (table hard : id_hard,name, ip, id_user) , (table os: id-os, libelle), (table user: id_user, name, mailuser).
if I insert data in table hard, there is an error message ("cannot add or update a child row: a foreign key constraint fails ('boky'.'hard', CONSTRAINT 'FK_hard_id_user' FOREING KEY ('id_user') REFERENCES 'user' (id_user))"
this is my source code
Dim query As String = "INSERT INTO hard(name, ip, id_user) VALUES (?,?,?)"
Dim query 2 As String = "SELECT @@Identity"
Dim connstring As String = "server=localhost;UID=Dohery;Database=boky;Password=122346"
Dim Id As Integer
Using conn As New MySqlConnection (connstring)
Using cmd As New MySqlCommand(query,conn)
cmd.Parameters.AddWithValue("",TextBox11.Text)
cmd.Parameters.AddWithValue("",TextBox9.Text)
cmd.Parameters.AddWithValue("",ComboBox6.Text)
conn.Open()
cmd.ExecuteQuery()
cmd.CommandText = query2
ID=cmd.ExecuteScalar()
End Using
End Using
Help me plezzzzzz, thanks.
modified 14-Mar-16 3:16am.
|
|
|
|
|
The database is looking for a User_ID in your user table.
You need to make sure the User_ID exists in the user table BEFORE you insert the hard record.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
The bright side - it's Friday
I have a datagridview that is databound with a bindingsource to a strongly typed dataset. There are 5 columns: 2 text fields, 2 combos (that have a datarelation to child tables) and a checkbox.
If I click to add a new row and manually type\select values - every thing works as expected. Meaning, as I am filling in data, there is another blank row below that is added. To help streamline data entry, I want to specify default values for the fields.
If I attempt to default values via dgv_DefaultValuesNeeded or the bs.AddingNew: All the default values are displayed correctly - but a new blank row isn't added to the end of the datagridview.
The last visible row (has all values defaulted correctly) but there isn't a blank line below? If I double click a combobox (don't even change the value - just double click it) or click the checkbox, then the change is reflected properly to the bindingsource and a new black line is added below?
I'm not sure what is going on. The comboboxes are set properly because the DisplayMember values are correct. If I click a previous row (without clicking the checkbox or selecting a combobox) the new row is removed as well - which makes it seem like it's not actually being added to the bindingsource during the AddingNew event.
Very stumped on this. Curious if anyone else has run into this. Probably missing something overly simple.
Private Sub bsPlantLocSub_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles bsPlantLocSub.AddingNew
Dim strTemp As String = GetNewDefaultValue_LabelPrefix()
dsLocSetup.DataTable_PlantLocSub.PlantLocationIDColumn.DefaultValue = _Default_PlantLocID
dsLocSetup.DataTable_PlantLocSub.LabelPrefixColumn.DefaultValue = strTemp
dsLocSetup.DataTable_PlantLocSub.LocationSubColumn.DefaultValue = "Location:" & Strings.Right(strTemp, 4)
dsLocSetup.DataTable_PlantLocSub.ComponentStatusColumn.DefaultValue = "IC"
dsLocSetup.DataTable_PlantLocSub.SysLocOnlyColumn.DefaultValue = False
bsPlantLocSub.MoveLast()
End Sub
I had also tried this in the bs.AddingNew - same behavior as listed above.
Dim dv As DataView = TryCast(bsPlantLocSub.List, DataView)
Dim drv As DataRowView = dv.AddNew
Dim strTemp As String = GetNewDefaultValue_LabelPrefix()
drv("PlantLocationID") = _Default_PlantLocID
drv("LabelPrefix") = strTemp
drv("LocationSub") = "Location:" & Strings.Right(strTemp, 4)
drv("ComponentStatus") = "IC"
drv("SysLocOnly") = False
e.NewObject = drv
bsPlantLocSub.MoveLast()
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|
|
I could lookup the DataGridView on MSDN and check, but in this case I am wondering whether you know what to expect from the default behaviour.
Jon_Boy wrote: The last visible row (has all values defaulted correctly) but there isn't a
blank line below? The new row with default-values is what you wanted, not an empty row. Make changes in that row and save it.
Jon_Boy wrote: but a new blank row isn't added to the end of the datagridview. An extra blank row would make the ones with the default values redundant. Overwrite any values you want there and save.
Jon_Boy wrote: If I double click a combobox (don't even change the value - just double click
it) or click the checkbox, then the change is reflected properly to the
bindingsource and a new black line is added below? Means you edited the row, and by changing focus it will be updating the bindingsource.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hey Eddy,
If all columns have defaulted values, wouldn't simply pressing tab on that row cause the new (blank) row to be added below? Why would a combo need to be selected or a text field need to go into edit mode to force the new row?
I tried to force a cell into edit mode after defaultvaluesneeded, but it causes a re-entrant condition, so the users is forced to do this.
Not a huge deal, but am a little confused when all the values are present.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|
|
Jon_Boy wrote: If all columns have defaulted values, wouldn't simply pressing tab on that row
cause the new (blank) row to be added below? I'd expect it to move the focus to the next control, as specified in the TabOrder.
Jon_Boy wrote: Why would a combo need to be selected or a text field need to go into edit mode
to force the new row? ..because you didn't edit anything. Press F2 to edit, ESC to cancel those edits. If you don't want that behaviour, set the EditMode[^] to DataGridViewEditMode.EditOnEnter .
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Thanks Eddy!
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|
|
You're welcome
|
|
|
|
|
conn.ConnectionString = "server=localhost;userid=root;password=;database=amwps"
Dim result As Integer
Dim dto As DateTime
Dim dfrom As datetime
Try
conn.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).FormattedValue <> "" Or row.Cells(1).FormattedValue <> "" Then
Dim sDateFrom As String
Dim sDateTo As Double
sDateFrom = CStr(row.Cells(3).FormattedValue)
sDateTo = CStr(row.Cells(4).FormattedValue)
tHrs = sDateFrom - sDateTo
strSql = "INSERT INTO tbl_attendance (id, employeeNumber, dates, timein, timeout, totalhrs, late) VALUES ('" _
& CStr(row.Cells(0).FormattedValue) & "','" _
& CStr(row.Cells(1).FormattedValue) & "','" _
& CStr(row.Cells(2).FormattedValue) & "','" _
& CStr(row.Cells(3).FormattedValue) & "','" _
& CStr(row.Cells(4).FormattedValue) & "','" _
& CStr(row.Cells(5).FormattedValue) & "','" _
& CStr(row.Cells(6).FormattedValue) & "')"
With cmd
.Connection = conn
.CommandText = strSql
End With
result = cmd.ExecuteNonQuery
End If
Next
If result = 0 Then
MsgBox("No saved Record.")
Else
MsgBox("All Records Saved.")
End If
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
conn.Close()
|
|
|
|
|
Dim sDateFrom As String
Dim sDateTo As Double
Can you see an anomaly in the above two lines? How do you think you can compute a value between a String and a Double ? There is a perfectly good DateTime type that you should use. It provides proper values, output display formatting, and the ability to compute time differences, and works in your code and your database. And, as you have already been told, do not use string concetenation to build SQL statements.
|
|
|
|
|
Your code is still vulnerable to SQL Injection[^].
Dim query As String = "INSERT INTO tbl_attendance (id, employeeNumber, dates, timein, timeout, totalhrs, late) VALUES (@id, @employeeNumber, @dates, @timein, @timeout, @totalhrs, @late)"
Dim result As Integer = 0
Using conn As New MySqlConnection("server=localhost;userid=root;password=;database=amwps")
Using cmd As New MySqlCommand(query, conn)
conn.Open()
For Each row As DataGridViewRow In DataGridView1.Rows
If row.Cells(0).FormattedValue <> "" Or row.Cells(1).FormattedValue <> "" Then
cmd.Parameters.AddWithValue("@id", row.Cells(0).Value)
cmd.Parameters.AddWithValue("@employeeNumber", row.Cells(1).Value)
cmd.Parameters.AddWithValue("@dates", row.Cells(2).Value)
cmd.Parameters.AddWithValue("@timein", row.Cells(3).Value)
cmd.Parameters.AddWithValue("@timeout", row.Cells(4).Value)
cmd.Parameters.AddWithValue("@totalhrs", row.Cells(5).Value)
cmd.Parameters.AddWithValue("@late", row.Cells(6).Value)
result += cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
End If
Next
End Using
End Using
If result = 0 Then
MsgBox("No saved Record.")
Else
MsgBox("All Records Saved.")
End If
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You can lead a horse to water ...
|
|
|
|
|
i need to calculate timediff between time in and time out then save it to the field name totalhrs. can you help me?
|
|
|
|
|
Go back and read my previous message, where I (try to) explain that you cannot calculate differences using a string and a float. And you should not even be using those types.
|
|
|
|
|
Imports MySql.Data.MySqlClient
Public Class frmCreditSubjs
Public conn As New MySqlConnection
Public cmd As New MySqlCommand
Public cmd1 As New MySqlCommand
Public dadapter As New MySqlDataAdapter
Public datardr As MySqlDataReader
Public strSql As String
Public datardr1 As MySqlDataReader
Public strSql1 As String
Public subjectcode1 As String
Public units1 As String
Public preReq1 As String
Public semester1 As String
Public year1 As String
Public count As Integer
Private Sub InsertLoad()
conn.ConnectionString = "server=localhost;userid=root;password=;database=aes"
Try
conn.Open()
strSql = "select subjectcode, units, prereq, semester, year from tbl_subjects"
cmd = New MySqlCommand(strSql, conn)
datardr = cmd.ExecuteReader()
While datardr.Read()
count += 1
subjectcode1 = datardr("subjectcode")
units1 = datardr("units")
preReq1 = datardr("prereq")
semester1 = datardr("semester")
year1 = datardr("year")
Try
strSql = "INSERT INTO tbl_studProspectus values ('', '" & textSNumber.Text & "','" & subjectcode1 & "', '" & units1 & "', '" & preReq1 & "', '" & semester1 & "', '" & year1 & "','')"
cmd = New MySqlCommand(strSql, conn)
datardr = cmd.ExecuteReader()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End While
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
Private Sub frmCreditSubjs_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
InsertLoad()
End Sub
End Class
|
|
|
|
|
Since the connection is being occupied by an active DataReader, it can't be used to do the INSERT queries. You have to open a separate connection to the SQL database to execute the INSERTs.
Now, since you're executing the INSERT in a try/catch block and it tried to give you an exception telling you that you're trying to reuse a connection that is already in use, you're catch block closed the connection, blocking the DataReader from executing any more.
Bottom line: You need TWO SqlConnections to the database. One for the DataReader and one for the INSERT commands.
|
|
|
|
|
sir can you give me some suggestion about it, thanks in advance.
|
|
|
|
|
sir it can be added but cannot loop .How can i make it ?
|
|
|
|
|
You already know how to do it. You already have the code to create a second connection object. You have supplied you'r own example! All you need to do is create a second connection object, surrounding your inner INSERT SqlCommand.
|
|
|
|
|
yes i know it, but i dont know how to create a second connection. will you please teach me sir?
|
|
|
|
|
Exactly like the first one, just assigning it to a separate variable.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
ok, thanks a lot guys. Godbless
|
|
|
|
|
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Also, don't store local variables as class-level fields. Declare them where you use them. And don't forget to wrap IDisposable objects in a Using block.
In this instance, there's no need to loop through the results of the query just to insert them into another table. Just use an INSERT .. SELECT statement[^]:
Dim query As String = "INSERT INTO tbl_studProspectus SELECT '', @StudentNumber, subjectcode, units, prereq, semester, year FROM tbl_subjects"
Using conn As New MySqlConnection("server=localhost;userid=root;password=;database=aes")
Using cmd As New MySqlCommand(query, conn)
cmd.Parameters.AddWithValue("@StudentNumber", textSNumber.Text)
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
i am, but it will always give me an error in my query near select. but i will try first this one, thanks
|
|
|
|
|
does all the data in tbl_subject will insert to tbl_prospectus?
|
|
|
|
|