I am having a hard time trying to populate a textbox, and two dropdown lists. What I am attempting to do is this: when the Company dropdownlist has been selected, it will display a grid of its respective values from a sqldatasource, which it does. Now the next step is once a selection from the company dropdown has been made, a textbox will display the membertype as well as the second dropdownlist. The third dropdown displays the groupid. All these three components have their outputs after a selection has been made. But I cannot seem to get the values to show. I also added code for clarification.
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Partial Class companydropdown
Inherits System.Web.UI.Page
Private Sub BindDropDownList(DropdownList1 As DropDownList, query As String, text As String, value As String, defaultText As String)
If Not IsPostBack Then
' Read sql server connection string from web.config file
Dim sConstr As String = ConfigurationManager.ConnectionStrings("ds17701ConnectionString").ConnectionString
Dim Conn As New SqlConnection(sConstr)
Dim dt As New DataTable("tbl")
Using Conn
Conn.Open()
Dim comm As New SqlCommand("SELECT CompanyName,CompanyID FROM CompanyList ORDER BY CompanyName", Conn)
Dim da As New SqlDataAdapter(comm)
'da.Fill(dt)
End Using
'DropdownList1.DataSource = dt
DropdownList1.DataTextField = "CompanyName"
DropdownList1.DataValueField = "CompanyID"
DropdownList1.DataBind()
'DropdownList1.Items.Insert(0, New ListItem("--Select--"))
End If
End Sub
Protected Sub Member_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
If DropDownList1.SelectedIndex > 0 Then
Dim sConstr As String = ConfigurationManager.ConnectionStrings("ds17701ConnectionString").ConnectionString
Dim Conn As New SqlConnection(sConstr)
Dim dt As New DataTable("tbl")
Using Conn
Conn.Open()
Dim comm As New SqlCommand("SELECT CompanyName,CompanyID FROM CompanyList " + DropDownList1.SelectedValue & " ORDER BY CompanyName", Conn)
Dim da As New SqlDataAdapter(comm)
da.Fill(dt)
End Using
'DropDownList2.DataSource = dt
DropDownList2.DataTextField = "MembershipStatus"
DropDownList2.DataValueField = "MemberTypeID"
' Bind sql server data into the Dropdown List
DropDownList2.DataBind()
Else
DropDownList2.Items.Clear()
End If
End Sub
Protected Sub Group_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
If DropDownList1.SelectedIndex > 0 Then
Dim sConstr As String = ConfigurationManager.ConnectionStrings("ds17701ConnectionString").ConnectionString
Dim Conn As New SqlConnection(sConstr)
Dim dt As New DataTable("tbl")
Using Conn
Conn.Open()
Dim comm As New SqlCommand("SELECT CompanyName,CompanyID FROM CompanyList " + DropDownList1.SelectedValue & " ORDER BY CompanyName", Conn)
Dim da As New SqlDataAdapter(comm)
da.Fill(dt)
End Using
'DropDownList2.DataSource = dt
DropDownList3.DataTextField = "GroupID"
DropDownList3.DataValueField = "GroupID"
' Bind sql server data into the Dropdown List
DropDownList3.DataBind()
Else
DropDownList3.Items.Clear()
End If
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList1.SelectedIndexChanged
DropDownList2.Enabled = False
'DropDownList3.Items.Clear()
DropDownList3.Items.Insert(0, New ListItem("", "0"))
Dim stateId As Integer = Integer.Parse(DropDownList1.SelectedItem.Value)
If stateId > 0 Then
Dim query As String = String.Format("", stateId)
BindDropDownList(DropDownList1, query, "CompanyName", "CompanyId", "")
DropDownList3.Enabled = True
DropDownList2.Enabled = True
End If
End Sub
End Class
Related
I have 4 dropdownlist in my web form that is cascading, the value of the 1st dropdownlist will affect the selection of the 2nd dropdownlist and so on.
Protected Sub line_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)
Dim level1_ID As Integer = Convert.ToInt32(line.SelectedValue.ToString())
FillProcess(level1_ID)
End Sub
Protected Sub ddlprocess_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)
Dim level2_ID As Integer = Convert.ToInt32(ddlprocess.SelectedValue.ToString())
FillEquipment(level2_ID)
End Sub
Protected Sub equipment_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)
Dim level3_ID As Integer = Convert.ToInt32(equipment.SelectedValue.ToString())
FillStep(level3_ID)
End Sub
Private Sub FillLine() '1st dropdown
Dim dt As New DataTable
Dim strSql = "select distinct level1_id, [LCODE1]+ ' | '+[LNAME1] as [LCODE1]" _
& " FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP]"
Using conn As New SqlConnection(ConStr),
cmd As New SqlCommand(strSql, conn)
conn.Open()
dt.Load(cmd.ExecuteReader)
End Using
If dt.Rows.Count > 0 Then
line.DataSource = dt
line.DataTextField = "LCODE1"
line.DataValueField = "level1_id"
line.DataBind()
line.Items.Insert(0, "")
End If
End Sub
Private Sub FillProcess(ByVal level1_ID As Integer) '2nd dropdown
Dim dt As New DataTable
Dim strSql = "select distinct level2_id, [LCODE2]+ ' | '+[LNAME2] as [LCODE2] " _
& "FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] where level1_id = #level1_id"
Using conn As New SqlConnection(ConStr),
cmd As New SqlCommand(strSql, conn)
cmd.Parameters.Add("#level1_id", SqlDbType.Int).Value = level1_ID
conn.Open()
dt.Load(cmd.ExecuteReader)
End Using
If dt.Rows.Count > 0 Then
ddlprocess.DataSource = dt
ddlprocess.DataTextField = "LCODE2"
ddlprocess.DataValueField = "level2_id"
ddlprocess.DataBind()
ddlprocess.Items.Insert(0, "")
End If
End Sub
Private Sub FillEquipment(ByVal level2_ID As Integer) '3rd dropdown
Dim dt As New DataTable
Dim strSql = "select distinct level3_id, [LCODE3]+ ' | '+[LNAME3] as [LCODE3] " _
& "FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] where level2_id = #level2_id"
Using conn As New SqlConnection(ConStr),
cmd As New SqlCommand(strSql, conn)
cmd.Parameters.Add("#level2_id", SqlDbType.Int).Value = level2_ID
conn.Open()
dt.Load(cmd.ExecuteReader)
End Using
If dt.Rows.Count > 0 Then
equipment.DataSource = dt
equipment.DataTextField = "LCODE3"
equipment.DataValueField = "level3_id"
equipment.DataBind()
equipment.Items.Insert(0, "")
End If
End Sub
Private Sub FillStep(ByVal level3_ID As Integer) '4th dropdown
Dim dt As New DataTable
Dim strSql = "select distinct [LCODE4]+ ' | '+[LNAME4] as [LCODE4] " _
& "FROM [SQLIOT].[dbo].[ZVIEW_MCM_LEVEL_LOOKUP] where level3_id = #level3_id"
Using conn As New SqlConnection(ConStr),
cmd As New SqlCommand(strSql, conn)
cmd.Parameters.Add("#level3_id", SqlDbType.Int).Value = level3_ID
conn.Open()
dt.Load(cmd.ExecuteReader)
End Using
If dt.Rows.Count > 0 Then
[step].DataSource = dt
[step].DataTextField = "LCODE4"
[step].DataBind()
[step].Items.Insert(0, "")
End If
End Sub
The reason I want a blank 1st row is so that when the form loads I want all dropdownlist to start off empty. During testing I thought to try selecting back to blank 1st row after selecting a value. This causes an error to occur at:
'Any one of the selectedindexchanged sub depending on which list I used
Protected Sub line_selectedindexchanged(ByVal sender As Object, ByVal e As EventArgs)
Dim level1_ID As Integer = Convert.ToInt32(line.SelectedValue.ToString()) '--> issue here
FillProcess(level1_ID)
End Sub
Error message being:
Input string was not in a correct format.
Anything I can do to fix this?
i defined some public variables in a class, when calling some variables in another class it works sometimes and sometimes not, the below code it works for the textbox but not in the form load, what i need is to run some sql sps to retrieve some data
Imports PZ_Project.DBConnClass
Imports System.Data.SqlClient
Imports System.Data
Public Class Main
Private Sub Main_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim UserTypeID As String = DBConnClass.LogedinUserType
MainMenu.Items.Add("Hi1")
Dim UserMainMenu As New SqlCommand("User_Menu_Items_App")
UserMainMenu.CommandType = CommandType.StoredProcedure
UserMainMenu.CommandText = "User_Menu_Items_App"
UserMainMenu.Connection = Conn
Dim UserTypeParam As SqlParameter = UserMainMenu.Parameters.Add("#intUserTypeID", LogedinUserType)
Dim SQLUserTypeTableAdaptor As SqlDataAdapter = New SqlDataAdapter(UserMainMenu)
Dim SQLUserTypeDataSet As DataSet = New DataSet()
SQLUserTypeTableAdaptor.Fill(SQLUserTypeDataSet)
MessageBox.Show("1" & UserTypeID)
For i = 0 To SQLUserTypeDataSet.Tables(0).Rows.Count - 1
MessageBox.Show(SQLUserTypeDataSet.Tables(0).Rows(i).Item(0))
MainMenu.Items.Add(SQLUserTypeDataSet.Tables(0).Rows(i).Item(0).ToString)
Next
' Dim obj As Object = UserMainMenu.ExecuteScalar()
'InitializeComponent()
'Dim SelectMenuItems As String
'Dim Menu_Item As String
'Dim tsmi As New ToolStripMenuItem("Users", Nothing)
'MenuStrip1.Items.Add(tsmi)
'SelectMenuItems = "Select Distinct User_Types.Name From Users Inner Join User_Types on User_Types.ID = Users.User_Type_ID Where User_Name = '" + User_Name.Text + "'"
'ConnCommand = New SqlCommand(UserName, Conn)
'Dim SQLUserTypeTableAdaptor As SqlDataAdapter = New SqlDataAdapter(ConnCommand)
'Dim SQLUserTypeDataSet As DataSet = New DataSet()
'SQLUserTypeTableAdaptor.Fill(SQLUserTypeDataSet)
'For i = 0 To SQLUserTypeDataSet.Tables(0).Rows.Count - 1
' UserType.Items.Add(SQLUserTypeDataSet.Tables(0).Rows(i).Item(0).ToString)
'Next
'UserType.Text = UserType.Items(0).ToString
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.MouseEnter
TextBox1.Text = LogedinUserType
MessageBox.Show(LogedinUserName & " / " & LogedinUserTypeName)
End Sub
End Class
Try to change this
Dim UserTypeID As String = DBConnClass.LogedinUserType
to this
Dim DBConnClass As New DBConnClass
Add this
Dim UserTypeID As String= DBConnClass.LogedinUserType
Im having trouble getting the correct data to display in a DataGridView. I have two tables (Student) and (Module) in an access db, I have the text boxes displaying the data from the Student tables but i need the DataGridView to display their corresponding modules. the code is
Public Class frnMain
Dim objConnection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = Students.accdb")
Dim objStudentDA As New OleDb.OleDbDataAdapter("Select * From Student", objConnection)
Dim objStudentCB As New OleDb.OleDbCommandBuilder(objStudentDA)
Dim objDataSet As New DataSet()
Dim objModuleDA As New OleDb.OleDbDataAdapter("Select * from Student", objConnection)
Dim objModuleCB As New OleDb.OleDbCommandBuilder(objModuleDA)
Dim Counter As Integer = 1
Private Sub frnMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Retrieve()
FillStudentDetails()
End Sub
Public Sub Retrieve()
objDataSet.Clear()
objStudentDA.FillSchema(objDataSet, SchemaType.Source, "Student")
objStudentDA.Fill(objDataSet, "student")
objModuleDA.FillSchema(objDataSet, SchemaType.Source, "Module")
objModuleDA.Fill(objDataSet, "Module")
'Set Relationships
objDataSet.Relations.Clear()
objDataSet.Relations.Add("student2Module", objDataSet.Tables("Student").Columns("StudentId"),
objDataSet.Tables("Module").Columns("StudentId"))
End Sub
Public Sub FillStudentDetails()
Dim objrow As DataRow
Dim objModule As DataRow
objrow = objDataSet.Tables("Student").Rows.Find(Counter)
objModule = objDataSet.Tables("Module").Rows.Find(Counter)
mtbStudentId.Text = objrow.Item("StudentId")
txtName.Text = objrow.Item("StudentName")
txtAddress.Text = objrow.Item("StudentAddress")
For Each objModules In objrow.GetChildRows("Student2Module")
dgvModule.DataSource = objDataSet.Tables(0)
Next
End Sub
I know the code is wrong in the for loop at the end I was just experimenting to see if i could get it. thanks in advance.
I fixed it by using fixed columns and just looping through the other table
dgvModule.ColumnCount = 3
dgvModule.Columns(0).Name = "Module ID"
dgvModule.Columns(1).Name = "Module Name"
dgvModule.Columns(2).Name = "Student Id "
dgvModule.Rows.Clear()
For i As Integer = 1 To objDataSet.Tables("Module").Rows.Count
objModule = objDataSet.Tables("Module").Rows.Find(i)
If currentId = objModule.Item("StudentId") Then
Dim row As String() = New String() {(objModule.Item("ModuleId")), objModule.Item("ModuleName"), objModule.Item("StudentId")}
dgvModule.Rows.Add(row)
Else
End If
Next
I hope some vb expert could tell me what I am doing wrong.
I am trying to open, modify and update a record in Northwind MDF.
The DA.Update(DS) statement always generate an error.
I find several examples on the net but only in C and thatI dond’t understand, only work in VB. I find myself so stuppid not to understand the update mechanism.
My source:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Call GETNWDS()
ContactNamebox.Text = ContactNamebox.Text + " " + Now
Call UpdateNWDS()
End If
End Sub
Public Sub GETNWDS()
'' open custumor DataAdapter daaset DS
Dim connectionString = ConfigurationManager.ConnectionStrings("NW testConnectionString").ConnectionString
Dim queryString As String = "SELECT CustomerID, CompanyName, ContactName FROM Customers where CustomerID='ALFKI'"
Dim CN As New SqlConnection(connectionString)
Dim DA As SqlDataAdapter = New SqlDataAdapter(queryString, CN)
Dim DS As DataSet = New DataSet
DA.Fill(DS)
Dim DT As DataTable = DS.Tables(0)
Dim row As DataRow = DT(0)
CustomerIDbox.Text = row("CustomerID").ToString
CompanyNamebox.Text = row("CompanyName").ToString
ContactNamebox.Text = row("ContactName").ToString
CN.Close()
End Sub
Public Sub UpdateNWDS()
Dim connectionString = ConfigurationManager.ConnectionStrings("NW testConnectionString").ConnectionString
Dim queryString As String = "SELECT CustomerID, CompanyName, ContactName FROM Customers where CustomerID='ALFKI'"
Dim CN As New SqlConnection(connectionString)
Dim DA As SqlDataAdapter = New SqlDataAdapter(queryString, CN)
Dim DS As DataSet = New DataSet
DA.Fill(DS)
Dim DT As DataTable = DS.Tables(0)
Dim row As DataRow = DT(0)
row("CustomerID") = CustomerIDbox.Text
row("CompanyName") = CompanyNamebox.Text
row("ContactName") = ContactNamebox.Text
Textbox.Text = row("ContactName").ToString
' DA.Update(DS) ' skip to avoud error
CN.Close()
End Sub
The webform is being filled with 3 DB items.
Then 1 box is changed and the update toutineis called but there is an error on the update statement.
Skippimg this statement shows this webform.
Note that the new row value is displalyed in a textbox.
I am working on an application that uses excel files as its data source. I would love the DataGridView to populate with the columns of a sheet when a worksheet name is selected from the drop down list.
Here is what I have tried doing:
Imports System.Data.OleDb
Public Class Form101
Public cn As New OleDbConnection
Public cm As New OleDbCommand
Public da As OleDbDataAdapter
Dim comb As String
Public dt As New DataTable
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Comb1.SelectedIndexChanged
comb = Comb1.SelectedText
End Sub
Public Sub FillDataGridView(ByVal Query As String)
da = New OleDbDataAdapter(Query, cn)
dt.Clear()
da.Fill(dt)
With DataGridView1
.DataSource = dt
.Columns(0).HeaderText = "Date"
.Columns(1).HeaderText = "Qty brought"
.Columns(2).HeaderText = "Qty sold"
.Columns(3).HeaderText = "Goods balance"
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
cn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\toojah app\Stock card.xls; Extended Properties= Excel 8.0;"
cn.Open()
FillDataGridView("select * FROM ['" & comb & "'] ")
End Sub
End Class
Try something like this. The first function will collect all of the columns and records in your excel file and place it into a datatable. Then If you wanted to add additional columns to the datable you can do it in the Public Sub CreateDataGridView. This has been tested and works.
Friend Shared Function BuildDatatable () as datatable
Dim dt As New DataTable
Dim Conn As System.Data.OleDb.OleDbConnection
Dim cmd As System.Data.OleDb.OleDbDataAdapter
dim MyFile as string = "C:\toojah app\Stock card.xls"
Conn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + MyFile + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';")
Conn.Open()
Dim dtSheets As DataTable = Conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim listSheet As New List(Of String)
Dim drSheet As DataRow
For Each drSheet In dtSheets.Rows
listSheet.Add(drSheet("TABLE_NAME").ToString())
cmd = New System.Data.OleDb.OleDbDataAdapter("select * from [" & drSheet("TABLE_NAME").ToString() & "]", Conn)
cmd.TableMappings.Add("Table", "Net-informations.com")
cmd.Fill(dt)
Conn.Close()
Next
Return dt
Catch ex As Exception
Return Nothing
End Try
End Function
'This is used to pass the function datatable as a dt to then pass to the public sub as shown below.
Dim dt As DataTable = BuildDatatable
Public Sub CreateDataGridView(dt)
Dim newColumn As New Data.DataColumn("ComeColumnName", GetType(System.String))
newColumn.DefaultValue = "YourValues"
dt.Columns.Add(newColumn)
DataGridView1.DataSource = dt
End Sub