views:

125

answers:

2

This is a winform and I'm using mysql as a database, here is my code: I'm trying to add data into multiple tables.

If TextBox14.Text = "" Or TextBox7.Text = "" Or TextBox10.Text = "" Then
            MsgBox("Please fill up the fields with a labels in bold letters!", MsgBoxStyle.Information)


            cn = New MySqlConnection("Server=localhost; Database=school;Uid=root;Pwd=nitoryolai123$%^;")
            'provider to be used when working with access database
            cn.Open()
            cmd = New MySqlCommand("select * from parents, mother, father", cn)



            cmd.CommandText = "insert into parents values('" + idnum + "','" + p_contact + "','" + p_ad + "')"
            cmd.CommandText = "insert into mother values('" + idnum + "','" + mother + "','" + mother_occu + "')"
            cmd.CommandText = "insert into  father values('" + idnum + "','" + father + "',''" + father_occu + "')"


            cmd.ExecuteNonQuery()

I get this error, please help: Index and length must refer to a location within the string. Parameter name: length

A: 
    Dim sql As String
    sql = "insert into parents (`id`,`contact`,`ad`) values('" + idnum + "','" + p_contact + "','" + p_ad + "')"
    sql = sql & "; insert into mother (`id`,`mom`,`occ`) values('" + idnum + "','" + mother + "','" + mother_occu + "')"
    sql = sql & "; insert into father (`id`,`dad`,`occ`) values('" + idnum + "','" + father + "',''" + father_occu + "')"
    cmd.CommandText = sql
machine elf
cmd = New MySqlCommand("select * from parents, mother, father", cn) probably doesn't need that select does it?
machine elf
A select isn't a non-query...
machine elf
Is idnum numeric?
machine elf
I declared it as string
A: 

I think you have to do like this

cmd = New MySqlCommand()

 cmd.CommandText = "insert into parents values('" + idnum + "','" + p_contact + "','" + p_ad + "')"  

            cmd.ExecuteNonQuery() 

            cmd.CommandText = "insert into mother values('" + idnum + "','" + mother + "','" + mother_occu + "')" 

           cmd.ExecuteNonQuery() 

            cmd.CommandText = "insert into  father values('" + idnum + "','" + father + "',''" + father_occu + "')"    

            cmd.ExecuteNonQuery() 
Hojo
it does not even bother to execute the command when I do this.
@225269, you're welcome.
machine elf