DataGrid not showing data - wpf

I have a DataGrid in my UserControl and i am filling it dynamically . Using this code :
conn.Open()
cmd = New OdbcCommand("select dum,I_name from tbl_items where H=1", conn)
Dim da As New OdbcDataAdapter(cmd)
Dim dt As New DataTable("head")
da.Fill(dt)
dgv_itemHead.ItemsSource = New DataView(dt)
conn.Close()
It dose not show data i only shows two lines in it . i have one row in my table ..
Why it is happening ? how to solve this problem

Related

Data binding WPF populating combobox from mysql database Vb.net

Hi am converting my old winform app to WPF. Just getting started with it.
How do bind data from mysql table to a combobox. Below is my winform code which works.
Try
mysqlcnx.Open()
Dim da As New MySqlDataAdapter("select * from mydb.eventlist", mysqlcnx)
Dim table As New DataTable()
da.Fill(table)
ComboBox1.DataSource = New BindingSource(table, Nothing)
ComboBox1.DisplayMember = "EventName"
ComboBox1.SelectedIndex = -1
Catch ex As Exception
MsgBox(ex.Message)
End Try
I tried converting it like this but doesnt work.
Try
conn.Open()
Dim da As New MySqlDataAdapter("select * from mydb.eventlist", conn)
Dim table As New System.Data.DataTable()
da.Fill(table)
'EventcomboCon.ItemsSource = New BindingGroup(table, Nothing)
EventcomboCon.DisplayMemberPath = "EventName"
EventcomboCon.SelectedIndex = -1
Catch ex As Exception
MsgBox(ex.Message)
End Try
By searching the web, I was able to determine that the code should be like this:
With myComboBox
.DisplayMemberPath = "EventName"
.ItemsSource = myDataTable.AsEnumerable()
End With
You should have been able to find that same information.

Crystal Report not showing data from SQL Server 17.5

After creating a table and putting data in it in the previous form, this form tries to display the report, but I am getting a blank report. What am I doing wrong?
Here is my VB 2017 code for form load event:
Imports CrystalDecisions.CrystalReports.Engine
Public Class Form5
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cryRpt As New ReportDocument
cryRpt.Load("C:\Users\Administrator\source\repos\WindowsApp3\WindowsApp3\CrystalReport4.rpt")
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class
I solved my problem in the following way
Here is my modified VB 2017 code for form load event:
Dim query As String = "SELECT * FROM monAtt"
Dim cmd As New SqlCommand(query, conn1)
cmd.CommandType = CommandType.Text
conn1.Open()
Dim MyDA As New SqlClient.SqlDataAdapter()
MyDA.SelectCommand = cmd
Dim myDS As New TestDataSet1()
MyDA.Fill(myDS, "monAtt")
Dim oRpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
oRpt.Load("C:\Users\Administrator\source\repos\WindowsApp3\WindowsApp3\CrystalReport4.rpt")
oRpt.SetDataSource(myDS.Tables("monAtt"))
CrystalReportViewer1.ReportSource = oRpt
CrystalReportViewer1.RefreshReport()
CrystalReportViewer1.Visible = True
conn1.Close()
cmd.Dispose()

The connection was not closed the connection current state is open

Hye... I currently doing my final year project using vb.net and i got this error. Im tryin' to fix the error but still not succeed. I use ms access for database in my project. I try to put con.Open() before the 'dt' statement and con.Close() after the 'cboProduct.Select' but the result is the same. Really appreciate your helps. Thanks :)
'GLOBAL DECLARATIONS
Dim conString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Acer User\Documents\MAKLUMAT IVENTORI.accdb"
Dim con As OleDbConnection = New OleDbConnection(conString)
Dim adapter As New OleDbDataAdapter
Dim cmd As New OleDbCommand
Dim dt As New DataTable
Dim ds As New DataSet
Private Sub RefreshData()
dt = New DataTable
ds = New DataSet
ds.Tables.Add(dt)
adapter = New OleDbDataAdapter("Select * FROM product WHERE lab_kod='66'", con)
adapter.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
labelID.Text = getAutoID()
lblLabCode.Text = "66"
cboProduct.Select()
Dim v_SQL As String = "SELECT * FROM kategori_product"
cmd = New OleDbCommand(v_SQL, con)
con.Open()
Dim v_dataReader As OleDbDataReader = cmd.ExecuteReader()
Dim v_dataTable As New DataTable
v_dataTable.Columns.Add("product_kod", Type.GetType("System.String"))
If v_dataReader.HasRows Then
While v_dataReader.Read
v_dataTable.Rows.Add(v_dataReader.GetString(0))
End While
cboProduct.DataSource = v_dataTable
End If
cboProduct.DisplayMember = "product_kod"
cboProduct.ValueMember = "product_kod"
v_dataReader.Close()
con.Close()
End Sub
Don't store short-lived objects, such as database connections, as global variables.
In fact, don't use global variables, ever, at all.
(though I note that assuming this is a Class and not a Module those are actually class fields, not globals, but you're still misusing them)
Your OleDbDataAdapter.Fill call requires the connection to be open, but you call .Fill(dt) before calling con.Open()
Use Using (using() in C#) blocks to ensure your database connection is always closed, even in the event of failure.

Dynamically creating a dropdown, with values from SQL server

I want to create a feature within my program that when i load in an excel file, it counts how many columns in the spreadsheet and dynamically creates a dropdown for each and every column, to select what header it should be, and bind it to the GridView .Data from SQL server will populate the dropdowns. Some problems i have run into is, how do i have a button where it asks for the file location? i have tried this way to load in a hardcoded location, but it doesnt work.
Sub SubmitBtn_Excel(Sender As Object, E As EventArgs)
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim path As String = "C:\\spreadsheet.xlsx"
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
dataSet = New System.Data.DataSet
MyCommand.Fill(dataSet)
GridView1.DataSource = dataSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
End Try
End Sub
I also have a method to call on the database with the table i have created to populate the data, should i put this method all in one method with the excel call?? considering that the dropdown id was dynamically created, it doesnt know how to find it because its not id'd in the HTML
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Page.IsPostBack Then
FillDeptDropdownList()
End If
End Sub
Protected Sub FillDeptDropdownList()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("MyDbCon").ConnectionString)
Dim cmd As New SqlCommand("Select * from demofeepay.dbo.catagories", con)
Dim adp As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
adp.Fill(dt)
ddlDynamic.DataSource = dt
ddlDynamic.DataTextField = "catagory"
ddlDynamic.DataValueField = "ID"
ddlDynamic.DataBind()
ddlDynamic.Items.Insert(0, "Select Catagory")
End Sub
This is the method i have for creating the dropdown
Sub SubmitBtn_Excel(Sender As Object, E As EventArgs)
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim path As String = "C:\\Users\\John\\Documents\\John\\EPP\\WorldNet_Notes\\CHRISTIAN_BROTHERS_HIGH_SCHOOL.xlsx"
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
dataSet = New System.Data.DataSet
MyCommand.Fill(dataSet)
GridView1.DataSource = dataSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
End Try
End Sub
This is the HTML
<form id="form1" runat="server" style="overflow-x:hidden; width:100%">
<div>
<asp:button id="butOK" text="Add Dropdowns" onclick="SubmitBtn_Click" runat="server"/>
<asp:button id="BtnAddExcel" text="Add Spreadsheet" OnClick="SubmitBtn_Excel" runat="server"/>
</div>
<div id ="ddlDynamic">
<!-- Dynamic Dropdowns -->
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
I am aware that this is all over the place, as you can see im a bit lost. I dont really want anybody to spoon fed me the answer, im just unsure how to structure it, or could anybody point me in the right direction to find out more. New to vb. Thanks in advance :-)
First you will need to add a PlaceHolder control to your WebForm.
You will add your DropDownLists to this PlaceHolder this way:
foreach (var item in collection)
{
DropDownList ddl = new DropDownList();
lb.Items.AddRange(item.values);
ddl.ID = "ddl" + (PlaceHolder1.Controls.Count() + 1);
PlaceHolder1.Controls.Add(ddl);
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
}
For your file button, you may want to use the FileUpload control.

Loading data from SQL Server row by row to textboxes

I am trying to retrieve data row by row from my SQL Server and load them into my respective textboxes, I was doing the below code but of course it doesn't work as the For Each loop will load every single textboxes with each data retrieved, ran out of ideas. Appreciate if someone can give me a boost here. Thanks.
Private Sub retrieve_Data()
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Try
con.ConnectionString = "Data Source=HPEnvy-HP; Initial Catalog=Cinema; User Id=<id>; Password=<password>;"
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT [movie_ID], [movie_Title] FROM [Movie_Table] ORDER BY [MOVIE_ID] "
Dim lrd As SqlDataReader = cmd.ExecuteReader()
While lrd.Read()
Dim reader As String = lrd(1).ToString
Dim arrLoad As New ArrayList
arrLoad.Add(lrd(1).ToString)
For i = 0 To arrLoad.Count - 1
For Each cCtrl As Control In Panel1.Controls
If TypeOf cCtrl Is TextBox Then
Dim txtBox As New TextBox
txtBox = cCtrl
txtBox.Text = arrLoad.Item(i)
End If
Next
Next
End While
Catch ex As Exception
MessageBox.Show("Error while retrieving records on table..." & ex.Message, "Load Records")
Finally
con.Close()
End Try
End Sub
you just "new" your textbox,
you didn't add your textbox to your form.
you should add some code like that:
this.Controls.Add(textbox);
or
this.Panel1.Controls.Add(textbox);

Resources