Click here to Skip to main content
15,888,177 members
Please Sign up or sign in to vote.
3.00/5 (1 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 :
VB
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.
VB
 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 7: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

Quote:
VB.NET
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.
 
Share this answer
 
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
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[^]
 
Share this answer
 
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900