ASP/VB6 to .NET conversion help - asp.net

i'm updating some VB6 code to .NET and have come across a small problem. i'm basically in an infinite loop because i don't know the .NET equivalent of RecordSet.MoveNext() (VB6). i've updated the use of a RecordSet with a DataSet
While Not _sqlADP.Fill(_dataSet) = 0
// code
// more code
// original VB6 code had _recordSET.MoveNext() here.
End While
how do i check for EOF here and exit the loop?

I believe you'd use something like the following:
_sqlADP.Fill(_dataSet)
For Each row As DataRow In _dataSet.Tables(0).Rows
Next
You might also want to consider using a DataReader, which is probably more analogous to the VB6 RecordSet, as I believe the ADO RecordSet has read-only, forward-only behavior by default. If you were using a DataReader your loop would look like:
While _dataReader.Read()
End While

This is C#, but you can get the idea...
foreach(DataRow row in _sqlADP.Fill(_dataSet).Tables[0].Rows)
{
// code here
}

i figured something out myself. it's a very simple solution that i'm okay with.
Dim rowCount = _sqlADP.Fill(_dataSet)
While Not rowCount = 0
// code
// more code
// original VB6 code had _recordSET.MoveNext() here.
rowCount = rowCount - 1
End While
Edit: nevermind, i went with Scott Mitchell's solution.

Related

DataRow, DataColumn and DataTable not found when converting from rc1 to rc2

Im converting my asp.net core project from rc1 to rc2. I am having trouble getting DataRow, DataColumn to work and now im getting this error on DataTables:
DataTable does not contain a constructor that contains 0 arguments;
I have already added these to the project.json file with no luck.
"System.Data.Common": "4.0.1-rc2-24027",
"System.Data.SqlClient": "4.1.0-rc2-24027"
Is there a work around?
Thanks
I'm facing the same issue as well. Funny thing is that Microsoft has ported the base layer i.e. DataTable but not the DataRow & DataColumn. See section "Considered for porting" at this link.
Depending on what you are dealing with, I guess we are forced to use List or something similar.
Do let me know if you were able to come up with a work-around.

Controls and null reference exception

I'm aware that the NullReferenceException is pretty much the equivalent to the check engine light on a car but with this particular case I can't seem to pinpoint why its not working properly, I've never really messed with controls so I'm a bit unfamiliar in with the technicalities of it. What I have works, but I keep getting the exception when I run a trycatch around it. Here is what I have.
Dim TypeControl As Control
TypeControl = MaterialHeader_Edit1.FindControl("cboType")
DBTable = MaterialStuff.GetMaterial(ID)
Using DBTable
If DBTable.Rows.Count > 0 Then
Try
CType(TypeControl, DropDownList).SelectedItem.Text = (DBTable.Rows(0).Item("MaterialTypeDescription").ToString)
Catch ex As NullReferenceException
trace.runAllErrorLogging(ex.ToString)
End Try
End If
A NullReferenceException doesn't have anything to do with "controls" specifically. It's just an indication that your code assumes an object exists when at runtime it doesn't exist. For example, if you do this:
TypeControl = MaterialHeader_Edit1.FindControl("cboType")
CType(TypeControl, DropDownList).SelectedItem.Text = ...
Then your code assumes that TypeControl has a value on the second line. If it doesn't, trying to use .SelectedItem will fail because TypeControl is null. So you're assuming that .FindControl() actually found something. It doesn't make that guarantee implicitly.
Instead of making this assumption, you should verify:
TypeControl = MaterialHeader_Edit1.FindControl("cboType")
If TypeControl Is Not Nothing Then
CType(TypeControl, DropDownList).SelectedItem.Text = ...
End If
That way the code only executes if there's a value that it can use. You can add an Else to handle the condition where no value is found. (Display an error? Log the error? Silently continue? It's up to you how the condition should be handled.)
There are two possible problems here:
1 - Does FindControl actually find the control you seek? Add a check in to make sure you are actually finding it:
Dim TypeControl As Control
TypeControl = MaterialHeader_Edit1.FindControl("cboType")
If TypeControl Is Nothing Then Debug.Writeline("Could not find control")
2 - The SelectedItem of the control could also be Nothing so you may need to add a check here:
Dim ddl = CType(TypeControl, DropDownList)
If ddl.SelectedItem Is Nothing Then Debug.Writeline("Could not find selectedItem")

asp.net VB Delete Multiple Rows in EF4

I'm struggling with deleting multiple rows. I'm learning and have managed new rows and edits but cant seem to nail delete. Can someone help please?
this is what I have:
If ViewState("QuoteGroupID") IsNot Nothing Then
Dim GQID As Integer = CInt(ViewState("QuoteGroupID"))
Using db As New quotingSystemDevEntities
Dim QuoteToDelete = (From q In db.QuotesGeneratedV2 Where q.QuoteGroupID = GQID Select q)
db.DeleteObject(QuoteToDelete)
db.SaveChanges()
End Using
End If
I'm getting the error "The object cannot be deleted because it was not found in the ObjectStateManager."
Thanks you for your time.
UPDATE
After much messing around I have figured it out. The info on the web was very difficult to make sense of so I went for the try everything until it works method... Might not be the perfect solution but it works better than the one that wasn't! Hopefully this info will help someone out:
If ViewState("QuoteGroupID") IsNot Nothing Then
Dim GQID As Integer = CInt(ViewState("QuoteGroupID"))
Using db As New quotingSystemDevEntities
Dim QuoteToDelete = (From q In db.QuotesGeneratedV2 Where q.QuoteGroupID = GQID Select q)
For Each item In QuoteToDelete
db.Detach(item)
db.Attach(item)
db.DeleteObject(item)
Next
db.SaveChanges()
End Using
End If
I think you missing ObjectContext.Attach Method. Try using that.
Also similar question can be found Entity Framework Delete Object Problem

What is the equivalent of ASP(VB) Server.CreateObject("ADOX.Catalog") in ASP.net (VB.net)

Hi
I am trying to convert a asp page to asp.net.
I changed everything except this one line
Set dbC = Server.CreateObject("ADOX.Catalog")
Can anyone suggest an equivalent to this ADOX.Catalog in asp.net
This dbC is being used in the following way in asp
Set cm = Server.CreateObject("ADODB.Command")
cm.CommandText = Request("qry")
sN = Request("sN")
Set tmpTable = dbC.Tables(sN)
if NOT IsObject(tmpTable) then
dbC.Views.Append sN, cm
else
tmpTble.Command = cm
end if
Although I do not have direct experience, this article seems like it would be of assistance to you:
How To Retrieve Schema Information by Using GetOleDbSchemaTable and Visual C# .NET
Try adding a reference into your project called, Microsoft ADO ext.6.0 for DLL and security or something similar.
Once you add it, you'd be able to write code like:
Dim dbc as New ADOX.Catalog
Ps: Not answering from a windows m/c to verify. Got this from another forum post

ASP.NET ExecuteReader: CommandText property has not been initialized

I keep getting the error ExecuteReader: CommandText property has not been initialized and i'm not sure why it points to this line in my code
dd1.DataSource = LogData.StermQ2(datelistquery).Tables(0).DefaultView
dd1.DataBind()
dd1.Items.Insert(0, new listitem("Any location", "%"))
The top line is the one with the error
Any help would be much appreciated
Thanks
Jamie
I suspect it has something to do with your data source:
LogData.StermQ2(datelistquery)
Typically, when I'm working with some a SQLDataSource, I have to initialize is like so:
mydatasource.SelectCommand = "select * from mytable"
dd1.DataBind()
SelectCommand is the CommandText is the query that you're passing in. If the property has not been initialized, perhaps you havn't created the command yet? Or perhaps the datasource itself has not been initialized yet.
The weird thing with it is that it only happened occasionally and if i emptied my cache it would work fine.
So I just disable caching on that page and it seems to be working (at the moment)
Also added in some Try Catch statements and seems to be ok
Thanks for the help though
Jamie

Resources