A Combo box has two properties
1.One which displays the items in the combo box called the DISPLAY MEMBER
2. Another one which stores the underlying ID for the Item which is being Displayed.
eg your table may have EmployeeId and EmployeeName.
You would like to display the EmployeeName in the ComboBox and when a user selects an employee name you would like to save the EmployeeID.
Function Fillcombo(ByVal SQL As String, ByVal CBO As ComboBox, ByVal DisplayMember As String, ByVal ValueMember As String)
Dim dt As New DataTable 'Create a New DataTable
Dim dc As New SqlCommand 'Create a New SQLCommand
Dim da As New SqlDataAdapter ' Create a New SQL Data Adapter
dc.CommandTimeout = 0
dc.CommandText = sql
da.SelectCommand = dc
dc.Connection = cn
da.Fill(dt) ‘ Use the DataAdapter to fill the DataTable with the records
CBO.DataSource = dt
CBO.DisplayMember = DisplayMember ‘ This is the item which will be displayed on the combo box eg Employee Name
CBO.ValueMember = ValueMember ‘ This is the value which is stored eg. employeeID
MsgBox(“Unable to Fill the Combobox:” & CBO.Name & ” with values” & “:” & Err.Description)
How to use the above function
Fillcombo(“Select EmployeeID,EmployeeName from Employees”, Me.EmployeeCombo, “EmployeeName”, “EmployeeID”)
In SQL Server if you attempt to modify a table structure through the Table Designer you would encounter this error.
There is a simple fix for it.
Step 1 Go to Tools > Options
Go to Designer > Table and Database Designers
and Untick the option
Step 3: Now you should be able to save the changes to your tables.
How to execute a Backend Procedure in SQL Server using VB.NET
Here is a function which allows you to execute a SQL Server Procedure in VB.NET
Function ExecuteProcedure_DeleteRecord() as boolean
Dim cn As New SqlConnection
Dim cmd As New SqlCommand
Dim sql As String
sql = "cs_Post_Delete"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@SectionID", "200") ' string then enclose it in the double quotes
cmd.Parameters.AddWithValue("@SettingsID", 1000) ' numeric value then you can put it without any quotes
cmd.Parameters.AddWithValue("@PostID", Me.TextBox1.Text) ' Here we are picking a value from a textbox
cmd.Parameters.AddWithValue("@Reason", "Deleted due to SPAM")
cmd.CommandText = sql
cmd.Connection = cn
'Call Open database - connect to the database
OpenConnection(cn) ' I use a seperate function to open a new connection
MsgBox("Unable to Delete the record:," & Err.Description)
CloseConnection(cn) ' I use a seperate function to close an existing connection
When the sql contains the single quotes then they have to be properly escaped otherwise it will give errors
just pass the field value which you want to check and the following function will return you the properly escaped string.
eg. o’brien will be returned as o”brien and this will not cause your sql to break.
Function FixSql(ByVal sStr As String) As String
FixSql = Replace(sStr, “‘”, “””)