MVCGrid.net MVCGridConfig.vb example - mvcgrid.net

Are there any examples available of MVCGrid.net using VB.NET?
Tried a number of methods, like this...
MVCGridDefinitionTable.Add("EmployeeGrid", New MVCGridBuilder(Of Person)().WithAuthorizationType(AuthorizationType.AllowAnonymous).AddColumns(Sub(cols)
cols.Add("Id").WithValueExpression(Function(p) p.Id.ToString())
cols.Add("FirstName").WithHeaderText("First Name").WithValueExpression(Function(p) p.FirstName)
cols.Add("LastName").WithHeaderText("Last Name").WithValueExpression(Function(p) p.LastName)
End Sub).WithRetrieveDataMethod(Function(options)
Dim result = New QueryResult(Of Person)()
Using db = New SampleDatabaseEntities()
result.Items = db.People.Where(Function(p) p.Employee).ToList()
End Using
Return result
End Function))
... but something is really still adrift. Any pointers, or an example, would be much appreciated.
Thank you

Have figured out part of it
MVCGridDefinitionTable.Add(Of GridModal)("CurrentBalanceGrid", New MVCGrid.Models.MVCGridBuilder(Of GridModal)().WithAuthorizationType(MVCGrid.Models.AuthorizationType.AllowAnonymous).AddColumns(Sub(cols)
cols.Add("Id").WithValueExpression(Function(p) p.TransactionID.ToString())
cols.Add("DebitAmount").WithHeaderText("Debit").WithValueExpression(Function(p) p.DebitAmount)
cols.Add("CreditAmount").WithHeaderText("Credit").WithValueExpression(Function(p) p.CreditAmount)
Just need to wire up the data now

Related

Got this error "Comma, ')', or a valid expression continuation expected"

can anyone help me to solve this error. Got error at line
wtp.Add(New WaterTreament() With {.position = positionName, .value = Convert.ToDouble(sqlRs2("value").ToString()), .timestamp = sqlRs2("dtimestamp").ToString()})
Here's the code
Dim wtp As New List(Of WaterTreament)()
sqlRs2.Open(strSQL2, objConn2)
If Not sqlRs2.EOF Then
Dim positionName = sqlRs2("position").ToString()
If (positionName = "60") Then
positionName = "CHLORINE"
ElseIf (positionName = "61") Then
positionName = "TURBIDITY"
ElseIf (positionName = "62") Then
positionName = "PH"
ElseIf (positionName = "63") Then
positionName = "FLORIDE"
End If
wtp.Add(New WaterTreament() With {.position = positionName, .value = Convert.ToDouble(sqlRs2("value").ToString()), .timestamp = sqlRs2("dtimestamp").ToString()})
End If
Well, sometimes it is REALLY a lot better to just write out a few extra lines of code. I mean, fancy pants can be cool, and great, but often a few extra lines of code is a bettter approach.
So, say try this:
dim MyWaterTreat as new WaterTrament
MyWaterTreat.position = positionName
MyWaterTreat.value = Convert.ToDouble(sqlRs2("Value).ToString())
MyWaterTreat.TimeStamp = sqlRs2("dtimesamp").ToString()
wtp.Add(MyWaterTreat)
You get much nicer intel-sense, and probably save a pot of coffee in the debugging process anyway.
So, sometimes, you better off to write a few extra lines of code. And with VS, then say ctr-d to duplicate the previous line? You might result in a bit more code but you the developer did not actually have to type in that code, or all that much of it.
So, create a new instance of the class. Add the values along with GREAT nice intel-sense, and then add that new object to the list.
Better yet, you also tend to get intel-sense for miss matched data types.

need help update a datatable cell value

I have a trivial question about updating a datatable MyDT. I googled and found several approach and got compile errors. Here is the code and here is what I tried with the error. Any help is greatly appreciated. BTW, I am using asp.net framework 2.0 and VB.NET
MyDT.Rows[1][4] = "4NF" ' Property access must assign to the property or use it value
row.Item("New_Column") = "4NF" ' Input string was not in a correct format.
Couldn't store <4NF> in New_Column Column. Expect type is Byte.
row["New_Column"] = "4NF" ' Expression is not a method
Dim StatusCode As String
For Each row As DataRow In MyDT.Rows
StatusCode= row.Item("ThisColumn").ToString()
If StatusCode= "NONF" Then
MyDT.Rows[1][4] = "4NF"
End If
Next row
You should either:
Store data as String in your DataTable
Do a conversion
Here how the conversion looks
MyDT.Rows[1][4] = Convert.ToByte("4NF")

VB.Net stuck with setvalue method of system reflection

A tons of advance thanks to everybody for taking time on this!
Am I doning this correctly? it says "Parameter count mismatch." for SetValue method
For i = 0 To (missingFieldName.Count) - 1
Dim propertyiInfo As System.Reflection.PropertyInfo =
GetType(ImportFields).GetProperty(missingValuesTakenfromUser.missingFieldAcquired(i, 0))
Dim fieldacquired As String = missingValuesTakenfromUser.missingFieldAcquired(i, 1)
propertyiInfo.SetValue(fields, fieldacquired, New Object() {0})
Next i
fields is an instance of class ImportFields. tried a 100 different ways to work with this in last 2 days. desperately looking for a solution, please someone?
For a non-indexed property, the third parameter should be null, not an empty array. Try this:
propertyiInfo.SetValue(fields, fieldacquired, null);
If you use .net 4.5, there's a new overload with only two parameters that you can use instead:
propertyiInfo.SetValue(fields, fieldacquired); // .net 4.5 only
Got it after 4 days continuous struggle. here is how to do that
Many thanks to Jods for giving a tip above.
For i = 0 To (missingFieldName.Count) - 1
Dim propertyiInfo As System.Reflection.PropertyInfo =
GetType(ImportFields).GetProperty(missingValuesTakenfromUser.missingFieldAcquired(i, 0))
Dim fieldacquired As String = missingValuesTakenfromUser.missingFieldAcquired(i, 1)
propertyiInfo.GetValue(fields, Nothing)(0) = fieldacquired
Next i
actually, MS is a bit confusing when they say indexed items. it meant to me they referring to array.
thanks a lot for everybody who struggled to solve this for me.

how to fix EntityReference error

I'm attempt to commit mostly all new objects to the database, apart from the user. I'm new to entity framework and im not sure how to combat this error.
Error on line _orderDetail.CalenderItems.Add(_newCalendarItem):
The object could not be added or attached because its EntityReference has an EntityKey property value that does not match the EntityKey for this object.
Code:
_db.Orders.AddObject(_order)
For Each n In _namelist
_db.Names.AddObject(n)
Next
For Each n In _namelist
For i As Integer = 1 To _copies
Dim _orderDetail As New OrderDetail
_db.OrderDetails.AddObject(_orderDetail)
_orderDetail.Name = n
_orderDetail.Order = _order
For Each c In _calendarItems
Dim _newCalendarItem As New CalenderItem
_newCalendarItem.Image = c.Image
_newCalendarItem.YearMonth = c.YearMonth
_orderDetail.CalenderItems.Add(_newCalendarItem)
Next
Next
Next
_db.SaveChanges()
I believe I need to add add an entity reference but I'm not sure how. Can anyone point me in the right direction
As dnndeveloper says, your answer is ObjectContext.CreateObject<T>.
So you're gonna want -
Dim ci = _db.CreateObject(Of CalenderItem)()
ci.OrderDetail = _orderDetail
ci.Image = c.image
ci.YearMonth = c.YearMonth
_orderDetail.CalenderItems.Add(ci)
or something along those lines. I've run into this issue a couple of times and this has worked so far.
HTH
Instead of creating a "new calendaritem" you should use _db.OrderDetails.CalendarItem.New() etc... either that or set _newCalendarItem.EntityKey to null.

How do you call a method from a variable in ASP Classic?

For example, how can I run me.test below?
myvar = 'test'
me.myvar
ASP looks for the method "myvar" and doesn't find it. In PHP I could simply say $me->$myvar but ASP's syntax doesn't distinguish between variables and methods. Suggestions?
Closely related to this, is there a method_exists function in ASP Classic?
Thanks in advance!
EDIT: I'm writing a validation class and would like to call a list of methods via a pipe delimited string.
So for example, to validate a name field, I'd call:
validate("required|min_length(3)|max_length(100)|alphanumeric")
I like the idea of having a single line that shows all the ways a given field is being validated. And each pipe delimited section of the string is the name of a method.
If you have suggestions for a better setup, I'm all ears!
You can achieve this in VBScript by using the GetRef function:-
Function Test(val)
Test = val & " has been tested"
End Function
Dim myvar : myvar = "Test"
Dim x : Set x = GetRef(myvar)
Response.Write x("Thing")
Will send "Thing has been tested" to the client.
So here is your validate requirement using GetRef:-
validate("Hello World", "min_length(3)|max_length(10)|alphanumeric")
Function required(val)
required = val <> Empty
End Function
Function min_length(val, params)
min_length = Len(val) >= CInt(params(0))
End Function
Function max_length(val, params)
max_length = Len(val) <= CInt(params(0))
End Function
Function alphanumeric(val)
Dim rgx : Set rgx = New RegExp
rgx.Pattern = "^[A-Za-z0-9]+$"
alphanumeric = rgx.Test(val)
End Function
Function validate(val, criterion)
Dim arrCriterion : arrCriterion = Split(criterion, "|")
Dim criteria
validate = True
For Each criteria in arrCriterion
Dim paramListPos : paramListPos = InStr(criteria, "(")
If paramListPos = 0 Then
validate = GetRef(criteria)(val)
Else
Dim paramList
paramList = Split(Mid(criteria, paramListPos + 1, Len(criteria) - paramListPos - 1), ",")
criteria = Left(criteria, paramListPos - 1)
validate = GetRef(criteria)(val, paramList)
End If
If Not validate Then Exit For
Next
End Function
Having provided this I have to say though that if you are familiar with PHP then JScript would be a better choice on the server. In Javascript you can call a method like this:-
function test(val) { return val + " has been tested"; )
var myvar = "test"
Response.Write(this[myvar]("Thing"))
If you are talking about VBScript, it doesn't have that kind of functionality. (at least not to my knowledge) I might attempt it like this :
Select myvar
case "test":
test
case "anotherSub":
anotherSub
else
defaultSub
end select
It's been a while since I wrote VBScript (thank god), so I'm not sure how good my syntax is.
EDIT-Another strategy
Personally, I would do the above, for security reasons. But if you absolutely do not like it, then you may want to try using different languages on your page. I have in the past used both Javascript AND VBScript on my Classic ASP pages (both server side), and was able to call functions declared in the other language from my current language. This came in especially handy when I wanted to do something with Regular Expressions, but was in VBScript.
You can try something like
<script language="vbscript" runat="server">
MyJavascriptEval myvar
</script>
<script language="javascript" runat="server">
function MyJavascriptEval( myExpression)
{
eval(myExpression);
}
/* OR
function MyJavascriptEval( myExpression)
{
var f = new Function(myExpression);
f();
}
*/
</script>
I didn't test this in a classic ASP page, but I think it's close enough that it will work with minor tweaks.
Use the "Execute" statement in ASP/VBScript.
Execute "Response.Write ""hello world"""
PHP's ability to dynamically call or create functions are hacks that lead to poor programming practices. You need to explain what you're trying to accomplish (not how) and learn the correct way to code.
Just because you can do something, doesn't make it right or a good idea.
ASP does not support late binding in this manner. What are you trying to do, in a larger sense? Explain that, and someone can show you how to accomplish it in asp.
Additionally, you might consider "objectifying" the validation functionality. Making classes is possible (though not widely used) in VB Script.
<%
Class User
' declare private class variable
Private m_userName
' declare the property
Public Property Get UserName
UserName = m_userName
End Property
Public Property Let UserName (strUserName)
m_userName = strUserName
End Property
' declare and define the method
Sub DisplayUserName
Response.Write UserName
End Sub
End Class
%>

Resources