Click here to Skip to main content
14,360,161 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello can someone tell me why i got this problem.
I'm trying to create 1 main form and inside i have panel that will load other form inside it. And i have the problem when my query is runned inside the database its filled with empty fields, looks like it doesnt read what has inside the textboxes and checkboxes are they checked if they are is "1"

Here is my main form calling inside the panel the other form :
Dim form2 As UsersAddCP = New UsersAddCP()
        form2.TopLevel = False
        form2.TopMost = True
        Dim form1 As AdminMenu = CType(Application.OpenForms("AdminMenu"), AdminMenu)
        Dim panel1 As Panel = CType(form1.Controls("panel1"), Panel)
        panel1.Controls.Clear()
        panel1.Controls.Add(form2)
        form2.Show()

And here is my query module to insert the information.
 Public Sub RegisterUser()
        Try
            With cmd
                .Connection = conn
                .CommandText = "INSERT INTO users (uname,upassword,ucode,ucreate,utools,usearch) VALUES (@user,@pass,@ucd,@crt,@uto,@usrh)"
                .Parameters.AddWithValue("@user", UsersAddCP.txtUsername.Text)
                .Parameters.AddWithValue("@pass", UsersAddCP.txtPassword.Text)
                .Parameters.AddWithValue("@ucd", UsersAddCP.AdminCheck.Checked)
                .Parameters.AddWithValue("@crt", UsersAddCP.CreateCheck.Checked)
                .Parameters.AddWithValue("@uto", UsersAddCP.ToolsCheck.Checked)
                .Parameters.AddWithValue("@usrh", UsersAddCP.SearchCheck.Checked)
                .ExecuteNonQuery()
                .Parameters.Clear()
                .CommandText = "INSERT INTO personal (pacc,pName,pEmail,pYazaki,pPhone,pPosition) VALUES (@user,@name,@email,@yazaki,@phone,@position)"
                .Parameters.AddWithValue("@user", UsersAddCP.txtUsername.Text)
                .Parameters.AddWithValue("@name", UsersAddCP.txtName.Text)
                .Parameters.AddWithValue("@email", UsersAddCP.txtEmail.Text)
                .Parameters.AddWithValue("@yazaki", UsersAddCP.txtyazaki.Text)
                .Parameters.AddWithValue("@phone", UsersAddCP.txtPhone.Text)
                .Parameters.AddWithValue("@position", UsersAddCP.txtPossition.Text)
                Result = .ExecuteNonQuery
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            If Result > 0 Then
                MessageBox.Show("Sign Up Successful", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                MsgBox("Failed to register user!")
            End If
            cmd.Dispose()
            If conn IsNot Nothing Then
                conn.Close()
            End If
        End Try
    End Sub
End Module


Problem is only when i use to load the form inside the panel.
If i use normal Form Loads and fill out the fields records are readed and inserted in database fields, but when i use the panel in database create empty fields..Strange maybe someone can help me out how to solve it?

What I have tried:

Normal loading form and insert records - works
Load in panel other form and insert records - give empty fields in database
Posted
Updated 8-Nov-19 8:53am
v2
Comments
Maciej Los 8-Nov-19 2:11am
   
I do not see the part of code reponsible for data reading...
Ralf Meier 8-Nov-19 3:29am
   
I can't check it at the moment (perhaps later) ... but in my opinion you can make a panel as member of the Controls-Collection of a Form but you can't make a Form as member of the Controls-Collection of a Panel (which in fact is a Control - but of course it is a Container-Control).
Your additional comments point me also in this direction ...
But a question : why do you want to have this (or why do you think that you need it in this way) ? You can also add each Control into the Controls-Collection of your Panel ...
diablo22 8-Nov-19 6:19am
   
Can you give more details or example what you mean, sorry I couldn't catch your point.
Thanks in advance.
Richard Deeming 8-Nov-19 14:36pm
   
NB: It looks like you're storing passwords in plain text. Don't do that.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]
diablo22 8-Nov-19 15:26pm
   
as far as i read the best option for anti-inject is to use parameters like i do in my function i don't know is it true so far or how to test to make sql inject to see is it going to work
diablo22 8-Nov-19 15:52pm
   
as you mention i looked around my code and you were right that i have left empty places for sql injection i just test it myself, so one idea that you mention i will give example so other people may found it usefull too.
Wrong and easy to sql injection query:
.CommandText = "SELECT * FROM users WHERE uname = '" & theForm.txtUsername.Text & "'"
must become like this:
.CommandText = "SELECT * FROM users WHERE uname = @UserName"
.Parameters.AddWithValue("@UserName", theForm.txtUsername.Text)
Rate this:
Please Sign up or sign in to vote.

Solution 2

Quote:
Dim form2 As UsersAddCP = New UsersAddCP()
...
.Parameters.AddWithValue("@user", UsersAddCP.txtUsername.Text)
You've bumped into one of the horrible aspects of VB.NET, which was designed for backwards-compatibility with VB6.

Why is there a default instance of every form in VB.Net but not in C#? - Stack Overflow[^]

UsersAddCP.txtUsername refers to the control on the hidden default instance of the UsersAddCP form. But you're not using that default instance; you're creating a specific instance of the form, and displaying that. So all of the controls on the default instance will be empty.

You'll need to store your form instance somewhere so that you can use it from, or pass it into, your RegisterUser method. The precise details of how you do that will depend on where the two code blocks from your question are in relation to each other, and how your RegisterUser method is called.
   
Comments
diablo22 8-Nov-19 14:27pm
   
so i need something in form2 when enter fields and click button to use in register some public definitions so can access to form2 information, can you give me a little help or example what must i do for better make the stuff works? even if its other method to make the register working with the panel option. Thank you
Richard Deeming 8-Nov-19 14:36pm
   
One option would be to try to read the form instance out of the panel's controls collection:
Dim form1 As AdminMenu = DirectCast(Application.OpenForms("AdminMenu"), AdminMenu)
Dim panel1 As Panel = DirectCast(form1.Controls("panel1"), Panel)
If panel1.Controls.Count <> 0 Then
    Dim form2 As UsersAddCP = TryCast(panel1.Controls(0), UsersAddCP)
    If form2 IsNot Nothing Then
        RegisterUser(form2)
    End If
End If
Public Sub RegisterUser(ByVal theForm As UsersAddCP)
    ...
        ' Instead of UsersAddCP.Control.Text, use theForm.Control.Text:
        .Parameters.AddWithValue("@user", theForm.txtUsername.Text)
    ...
End Sub
diablo22 8-Nov-19 14:48pm
   
when i do it to change it to like this:
Public Sub RegisterUser(ByVal theForm As UsersAddCP)
...
' Instead of UsersAddCP.Control.Text, use theForm.Control.Text:
.Parameters.AddWithValue("@user", theForm.txtUsername.Text)
...
End Sub

i got underline txtUsername is not a member or UserAddCP
Richard Deeming 8-Nov-19 14:57pm
   
That doesn't make sense - if it's available via the default form instance (UsersAddCP.txtUsername), then it should be available via a specific instance (theForm.txtUsername).

NB: The version of RegisterUser I posted is an abbreviated example, just to demonstrate that you should replace the default form instance with the instance passed in as a parameter. It wasn't meant to replace the entire method body. :)
diablo22 8-Nov-19 15:00pm
   
i found it the problem, just visual studio give errors some times and need restart to functional correct.
But your idea was perfect..to give in Function Public Sub RegisterUser(ByVal theForm As UsersAddCP) string to use
Then in button click i just use RegisterUser(me) ,because i cannot use same name it must be me, then the function is called correct and read correct the theForm.txtUsername.Text)
Perfect will make a little tests more to view it and will report back in few minutes
diablo22 8-Nov-19 15:09pm
   
great now all is fine i found that i cannot use here the same for bouth calls
.CommandText = "INSERT INTO users (uname,upassword,ucode,ucreate,utools,usearch) VALUES (@user,@pass,@ucd,@crt,@uto,@usrh)"
.Parameters.AddWithValue("@user", theForm.txtUsername.Text)
.Parameters.AddWithValue("@pass", theForm.txtPassword.Text)
.Parameters.AddWithValue("@ucd", theForm.AdminCheck.Checked)
.Parameters.AddWithValue("@crt", theForm.CreateCheck.Checked)
.Parameters.AddWithValue("@uto", theForm.ToolsCheck.Checked)
.Parameters.AddWithValue("@usrh", theForm.SearchCheck.Checked)
.ExecuteNonQuery()
.Parameters.Clear()
.CommandText = "INSERT INTO personal (pacc,pName,pEmail,pYazaki,pPhone,pPosition) VALUES (@userx,@name,@email,@yazaki,@phone,@position)"
.Parameters.AddWithValue("@userx", theForm.txtUsername.Text)
because it gives error parameter is already defined so i changed it to -> userx and all fine. Thank you
Richard Deeming 8-Nov-19 15:14pm
   
You're reusing the same database command and connection objects each time you call the method. You shouldn't do that. Instead, create the connection and command objects when you need them, and wrap them in Using blocks to ensure they're always disposed of properly.

Using Statement (Visual Basic) | Microsoft Docs[^]

Keeping the database objects hanging around in fields will lead to hard to track errors, and possibly even data corruption.
diablo22 8-Nov-19 15:28pm
   
correctly i fixed it and i do not need using blocks method my mistake was that i was using the same definition 2 times and the catch ex found it and replace it now works like a charm creation of accounts without problems. If user exists = sorry , if not create it.
Rate this:
Please Sign up or sign in to vote.

Solution 1

More people seem to do what you want, but as Ralf said, this is not good practice.
There is a lot of bad code on the internet, be careful what you choose.

See the first answer by OriginalGriff about using a UserControl here: How to Add the Form in Panel from another Form[^]

See: Creating a Simple User Control with Visual Basic[^]
If you prefer a video, see: Vb.Net UserControl - YouTube[^]

And here is an example of sharing data between multiple forms by using a Module: Visual Basic (VB) Share global variables between multiple forms using a Module - YouTube[^]

Here is a more elaborate example that shows how to separate your data from a form, by using PL (Presentation Layer), BLL (Business Logic Layer) and DAL (Data Access Layer):
Building an N-Tier Application in VB.NET, in 8 Steps[^]
   
v6
Comments
diablo22 8-Nov-19 4:24am
   
What I want to do is like this:
New Project>VB.net>Form1
In Form1 I have added Panel1
In form1 I have added 1 button to open form2 inside the Panel1 (that in source Form1 is named as AdminMenu)
The code is above to call the form2 (that in source is named UsersAddCP)

So what I have in Form2 (UsersAddCP)
I have 7 textboxes + 4 checkboxes + 1 button to save

Okay so lets begin,
When I click in form1 the button to call the form2 inside the panel1 its working fine.
But when I try to write in the textboxes something and click the button.
The button calls for the function RegisterUser() and inside the database it create new record but with empty fields.
Okay what I try I try to put on Form2 all textboxes (Text = "some default text") and I run the program and without to write anything to the textboxes because they have now default value text and click the button it create in database the records correctly.

So how to fix this problem when I try to enter information to the textbox to record it to the database?
RickZeeland 8-Nov-19 4:39am
   
Do not use a form in a panel, it is bad design !
See the updated solution for a link to "Creating a Simple User Control with Visual Basic"
diablo22 8-Nov-19 4:51am
   
there is no way to solve this problem, because I want to make a more effected program not to display new forms and close previous and stuff like this, something like 1 main form and display features from other forms fill out the information save read that's it not possible?
RickZeeland 8-Nov-19 4:54am
   
I will try to find an example for you, but please no more putting forms into panels :)
RickZeeland 8-Nov-19 5:17am
   
Could not find a good VB.Net example, but only a simple one that shows how to share data between forms.
diablo22 8-Nov-19 5:55am
   
thanks I will check it out when I get back at home, because now i'm at work and cannot view it. Maybe the problem is with read/write permissions between the forms and doesn't get what I type in form2 when its called and pressed the button while its active inside the form1. As you mention maybe there is solution for this with pass the values between forms
diablo22 8-Nov-19 11:34am
   
i had viewed the video, but this is not what i need. I need when execute the module to read the textboxes information, and this video is to send information from 1 form to other in fact this is not what i need, i need when its called the form2 inside the form1 to read the information from textboxes when press the button inside the form2 to create the new account

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100