Patrick Younes Patrick Younes - 4 months ago 14
Vb.net Question

Autofill TextBox/Checkbox from a previous TextBox' value (VB.NET Database)

Note: I'm using Visual Studio, original work was on SQL Server, moved to VB.NET

I have a Textbox "ViewStatusTxt", next to it there's a Button "ViewStatusBtn"
Below it there's a TextBox "ViewNAMETxt", another TextBox "ViewACTIVITYTxt" and then a Checkbox "ModifyStatusCB"

I'm trying to auto-fill the Checkbox AND the Textbox based on the ID input there, however I really have no clue about it since I'm new to VB.NET

Here's the code used

Private Sub IDSearch(StatusViewBtn As String)
' ADD SEARCH QUERY PARAMETERS - WITH WILDCARDS
SQL.AddParam("@StatusViewBtn", StatusViewBtn)

'RUN QUERY - SEARCH GIVES THOSE RESULTS
SQL.ExecQuery(" SELECT
aID,
Name,
Status,
Activity

FROM
[dbo].[initialTable]

WHERE
aID = @StatusViewBtn

ORDER BY
aID ASC")
End Sub


That's the function's code, which is fully working since it's a smaller version of the same one I used in a Search Page

Here's the button's function, which I'm sure is where I'm having problems, unless I need to add a specific function to the ViewNAMETxt

Private Sub StatusViewBtn_Click(sender As Object, e As EventArgs) Handles StatusViewBtn.Click
IDSearch(StatusViewBtn.Text)
ViewNAMETxt.Text = SQL.ExecQuery("SELECT
Name
FROM
initialTable
WHERE
aID = @StatusViewBtn")
End Sub


And I haven't even started on the Checkbox, viewing how the first one caused me issues. Hopefully the solution would be similar to both of them.
Thanks for reading guys, and sorry for the newbie question

Answer

1- Suppose you have a table named YourTable(int KeyColumn, string StringColumn, boolean BooleanColumn)

2- Create a form and put 2 textboxes and a checkbox and a button on it. KeyColumnTextBox, StringColumnTextBox, BooelanColumnCheckBox, SearchButton

3- In click event handler for SearchButton put the codes:

Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
    Dim connection = New SqlConnection("Your Connection string here")
    Dim command = New SqlCommand("SELECT StringColumn, BooleanColumn FROM YourTable WHERE KeyColumn=@KeyColumn", connection)
    command.Parameters.Add(New SqlParameter("@KeyColumn", Int32.Parse(KeyColumnTextBox.Text)))
    connection.Open()
    Dim reader = command.ExecuteReader()
    While reader.Read()
        StringColumnTextBox.Text = reader.GetString(0)
        BooleanColumnCheckBox.Checked = reader.GetBoolean(1)
    End While
End Sub

Don't forget to Imports System.Data.SqlClient at top of your file.

Comments