|
The dispose methods does that for you. Simple call cn.Dispose() in your finally.
|
|
|
|
|
i think cn.dispose() is better
|
|
|
|
|
I would write it like this:
If you need an explaination on any reason why I did certain things, just let me know.
Public Shared Function GetData(ByRef SQL As String) As System.Data.DataSet
Dim cn As New System.Data.SqlClient.SqlConnection(ConnectionString)
Dim adp As New System.Data.SqlClient.SqlDataAdapter(SQL, cn)
Dim ds As New System.Data.DataSet
Try
If SQL.Length > 0 Then
cn.Open()
adp.Fill(ds)
End If
Finally
adp.Dispose()
cn.Dispose()
End Try
Return ds
End Function
|
|
|
|
|
Does Dispose() work if the variable is never initialized?
Example:
Dim MyObject As Object
MyObject.Dispose()
I suppose I could test it quick but does that throw an exception because of a null object reference?
Edit:
Ok tested with following code
Dim oObject As New Object
oObject = Nothing
oObject.Dispose()
Which threw a NullReferenceException. This is why I was wondering if it was safe to just put cn.Dispose() in the finally clause instead of enclosing it in If Not cn Is Nothing Then block. That way you are covered if for some reason it never got initialized. Unless I'm missing something.
|
|
|
|
|
Don't worry about the whatever = Nothing . Just call the .Dispose() method on it. As soon as the object goes out of scope, the reference is automatically dropped and the GC collects it, when it gets around to it.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I realize you don't have to set an object reference to nothing. That wasn't my point. The point was you can't call .Dispose() on an NullReference. So if for some reason the object never gets assigned a reference (say there is a memory allocation problem for some reason) then in the finally clause the program would crash if there is a object.dispose() statement.
|
|
|
|
|
There is no NullReference Exception in my example, I declare the variables as new at the top of the sub.
If there's a memory allocation problem at some point that prevents the variable from being initialized, then the user has way more problems than your app crashing
|
|
|
|
|
I am copying data from excel file to spreadsheet component in VB6 form.The problem is that the the orientation of text is not maintained while copying and I am not able to find any method to copy the format of the cell to the component
|
|
|
|
|
What component? It's entirely possible that the component doesn't support the formatting Excel uses.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Microsoft Office Web Component.I read somewhere that it usually supports all excel features that is why I was wondering for the above mentioned possibility
|
|
|
|
|
I have no idea if it'll do what you what. I've never used it.
But, from what I've read, I don't think it'll support copy and paste how you want.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I am display a data grid and have enclosed some code below.
How do I get the column width and other properties. Seem get readonly but I want column width, alignment etc as well.
Any ideas?
Dim dtTable As DataTable = New DataTable
Dim drDataRow As DataRow
Dim col1 As New DataColumn
col1.DataType = System.Type.GetType("System.Int32")
col1.ColumnName = "Test"
col1.ReadOnly = True
dtTable.Columns.Add(col1)
|
|
|
|
|
|
I have a datagrid but nothing is getting displayed on it. Here is the sample code. What am I doing wrong.
Private Sub setFloorAreaGridStyles()
'Apply custom formatting
Dim dgTableStyle As New DataGridTableStyle
Dim aCol1 As New DataGridTextBoxColumn
With aCol1
.HeaderText = "ID##"
.Width = 100
.TextBox.Enabled = False
.NullText = ""
End With
With dgTableStyle.GridColumnStyles
.Add(aCol1)
End With
DataGrid1.TableStyles.Add(dgTableStyle)
Exit Sub
End Sub
|
|
|
|
|
I think you are missing one thing
acol1.MappingName = "name of the field in datatable binding with the grid"
Give that name and you will see the data displayed. Let me know if there is still any probs regarding that.
Het Waghela
Be Humble in Victory and Strong in Defeat.
Het Waghela, Blog|Het Waghela DotNet Questions Link|More Links
|
|
|
|
|
Still seem to be getting a problem. It will properly be something simple or setting I have missed. Here is an example of some code. I have copied it from the Microsoft site.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'FormatDataGrid
AddDataGridTableStyle()
End Sub
Private Sub AddDataGridTableStyle()
' Create a new DataGridTableStyle and set MappingName.
Dim myGridStyle As DataGridTableStyle = _
New DataGridTableStyle
myGridStyle.MappingName = "Customers"
' Add two DataGridColumnStyle objects.
Dim colStyle1 As DataGridColumnStyle = _
New DataGridTextBoxColumn
colStyle1.MappingName = "firstName"
Dim colStyle2 As DataGridColumnStyle = _
New DataGridBoolColumn
colStyle2.MappingName = "Current"
' Add column styles to table style.
myGridStyle.GridColumnStyles.Add(colStyle1)
myGridStyle.GridColumnStyles.Add(colStyle2)
' Add the grid style to the GridStylesCollection.
DataGrid1.TableStyles.Add(myGridStyle)
End Sub
|
|
|
|
|
Here is the brief sample code which i placed and its working in my app. Steps i followed is
first bind grid with datasource (datatable in my case)
grd.DataSource = dtBase
next step is applying the custom table style as below from a seperate function,
Dim ldgTableStyle As DataGridTableStyle ''' Table Style Variable
Dim ldgColumnText As DataGridTextBoxColumn ''' Text Box Column Variable
Dim ldgColumnBool As DataGridBoolColumn ''' Boolean Column Variable
ldgTableStyle = New DataGridTableStyle
'''ldgTableStyle = GetTableStyle(blnSorting:=True) ''' this functions sets general properties like forecolor backcolor, sorting etc.
ldgColumnText = New DataGridTextBoxColumn
ldgColumnText.MappingName = "MyColumnMappingName"
ldgColumnText.HeaderText = "Header I want on Grid"
ldgColumnText.Width = 200
ldgColumnText.TextBox.MaxLength = 15
ldgColumnText.TextBox.TextAlign = HorizontalAlignment.Left
ldgColumnText.TextBox.Enabled = False
ldgColumnText.ReadOnly = True
Call ldgTableStyle.GridColumnStyles.Add(ldgColumnText)
ldgColumnBool = New DataGridBoolColumn
ldgColumnBool.MappingName = "BoolColMapName"
ldgColumnBool.HeaderText = "HeaderText"
ldgColumnBool.Width = 175
ldgColumnBool.ReadOnly = True
Call ldgTableStyle.GridColumnStyles.Add(ldgColumnBool)
Call grd.TableStyles.Clear()
Call grd.TableStyles.Add(ldgTableStyle)
thats it, and its working try seeing the code and figure out if you are missing anything from ur code.
Het Waghela
Be Humble in Victory and Strong in Defeat.
Het Waghela, Blog|Het Waghela DotNet Questions Link|More Links
|
|
|
|
|
I done the following.
Dim dt As DataTable = New DataTable
DataGrid1.DataSource = dt
What appears on my grid is the start of the datagrid but with no headings.
Sorry for being such a nuisance but once I have cracked this, it will solve a lot of problems.
|
|
|
|
|
|
The problem I seem to be having now, is that I can get values onto the datagrid but want to put another value in that is a calculation of 2 values in a field. How can this be done if this can not be mapped to datatable. This worked fine when I did the following.....In short all I want to do is increase/decrease column sizes in the grid but this cannot be done if I do the following
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGrid1.DataSource = CreateDataSource()
End Sub
Function CreateDataSource() As ICollection
Dim i As Integer
Dim IK As Integer
Dim objDA As OleDb.OleDbDataAdapter
Dim objDS As New DataSet
Dim sSQL As String
Dim sData(,) As String
Dim objDR As OleDb.OleDbDataReader
Dim dt As DataTable = New DataTable
Dim dr As DataRow
Dim col1 As New DataColumn
Dim col2 As New DataColumn
Dim col3 As New DataColumn
Dim col4 As New DataColumn
col1.DataType = System.Type.GetType("System.Int32")
col2.DataType = System.Type.GetType("System.String")
col3.DataType = System.Type.GetType("System.String")
col4.DataType = System.Type.GetType("System.Int32")
col1.ColumnName = "ID#"
col2.ColumnName = "Surname"
col3.ColumnName = "Forename"
col4.ColumnName = "Value"
col1.ReadOnly = True
col2.ReadOnly = True
col3.ReadOnly = True
col4.ReadOnly = True
dt.Columns.Add(col1)
dt.Columns.Add(col2)
dt.Columns.Add(col3)
dt.Columns.Add(col4)
i = 0
sSQL = ""
sSQL = "SELECT EmployeeID, LastName, FirstName FROM EMPLOYEES"
Dim cmdOLE As New OleDb.OleDbCommand(sSQL, sOleDbConnection)
ReDim sData(2, 0)
objDR = cmdOLE.ExecuteReader
Do Until objDR.Read = False
sData(0, i) = objDR("EmployeeID")
sData(1, i) = objDR("LastName")
sData(2, i) = objDR("FirstName")
i += 1
ReDim Preserve sData(2, i)
Loop
IK = i
objDR.Close()
i = 0
For i = 0 To IK - 1
dr = dt.NewRow
dr(0) = sData(0, i)
dr(1) = sData(1, i)
dr(2) = sData(2, i)
dr(3) = 15 * i
dt.Rows.Add(dr)
Next
Dim dv As DataView = New DataView(dt)
Return dv
End Function
|
|
|
|
|
While adding columnstyle, you can change the width of the column, i think it should work fine.
ldgColumnText = New DataGridTextBoxColumn
ldgColumnText.MappingName = "MyColumnMappingName"
ldgColumnText.HeaderText = "Header I want on Grid"
ldgColumnText.Width = 200
ldgColumnText.TextBox.MaxLength = 15
ldgColumnText.TextBox.TextAlign = HorizontalAlignment.Left
ldgColumnText.TextBox.Enabled = False
ldgColumnText.ReadOnly = True
Call ldgTableStyle.GridColumnStyles.Add(ldgColumnText)
I have made line of code in bold which i think allows u to change the width of the column.
try and let me know the exact problem, if still it persists.
Het Waghela
Be Humble in Victory and Strong in Defeat.
Het Waghela, Blog|Het Waghela DotNet Questions Link|More Links
|
|
|
|
|
Hi,
I have problem when trying to print Crystal report from vb.net.
Program is very simpel : He accepts 4 parameters from 1 ComboBox and 3 TextBoxes and that should be printed in Crystal Reports report, but constantly shows " An unhandled exception of type 'System.ArgumentException' occurred in system.drawing.dll
Additional information: '-1' is not a valid value for 'value'. 'value' must be greater than or equal to 0. "
the code is below, thanks in advance for help.
Dim crReportDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim paramFields As New CrystalDecisions.Shared.ParameterFields
Dim paramField As New CrystalDecisions.Shared.ParameterField
Dim discreteVal As New CrystalDecisions.Shared.ParameterDiscreteValue
paramField.ParameterFieldName = "txtPrefix"
discreteVal.Value = cboPrefix.SelectedText
paramField.CurrentValues.Add(discreteVal)
discreteVal = New CrystalDecisions.Shared.ParameterDiscreteValue
paramField.ParameterFieldName = "txtDate"
discreteVal.Value = txtDate.Text
paramField.CurrentValues.Add(discreteVal)
discreteVal = New CrystalDecisions.Shared.ParameterDiscreteValue
paramField.ParameterFieldName = "txtpartnr"
discreteVal.Value = txtPartNr.Text
paramField.CurrentValues.Add(discreteVal)
discreteVal = New CrystalDecisions.Shared.ParameterDiscreteValue
paramField.ParameterFieldName = "txtPickingLocation"
discreteVal.Value = txtPickingLocation.Text
paramField.CurrentValues.Add(discreteVal)
paramFields.Add(paramField)
CrystalReportViewer1.ParameterFieldInfo = paramFields
CrystalReportViewer1.ReportSource = "C:\DotNetApps\Printlabel\WindowsApplication1\kistlabel.rpt"
CrystalReportViewer1.PrintReport()
|
|
|
|
|
I am using a vc++ dll in a vb program. The function require a pass byref single type. because in vc++ it require a floating point pointer. However, the values I'm getting is wrong. Anyone got any idea what could be wrong? I guessing the values are read in the wrong direction.
Thanks for any help.
Aaron
|
|
|
|
|
float* is not equal to single byref .
byref means the value is passed by reference, where pointers don't. it should be float& instead...
moreover, i'm not certain that float is the equivalent to single . isn't float (vb type) better ?
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
I have check on msdn, there is no float type for vb 6.0. Not sure about .net.
I've look up several papers saying that when the vc function in the dll is using a pointer as a parameter, the vb-side of calling the function should be using byref.
Aaron
|
|
|
|