Autogenerated id using c#

Posted by Amar888 under C# on 7/19/2010 | Views : 3763 | Status : [Member] | Replies : 2
hi,
it is a vb code but, i want c# code same format ?
i convert online using converter but its not working, its enter duplicate id .

Public Function GenerateAutoID() As String
Dim strQry As String
Dim strWhereQry As String
Dim strMaxID As String
Dim intLength As Integer
Dim lngID As Long
Dim objRet As Object

If Me.AcademicYear <> "" Then
strWhereQry = Me.SchoolType & "\" & Me.SchoolTransCode & "\" & Me.FormTransCode & "\" & Me.AcademicYear
Else
strWhereQry = Me.SchoolType & "\" & Me.SchoolTransCode & "\" & Me.FormTransCode
End If
strQry = "Select Max(" & Me.IDColumnName & ") from " & Me.IDTableName & " where " & Me.IDColumnName & " like '" & strWhereQry & "%'"
objRet = SqlHelper.ExecuteScalar(strConnection, CommandType.Text, strQry)

If IsDBNull(objRet) = False Then
strMaxID = CStr(objRet)
intLength = Len(strMaxID)
lngID = CLng(Mid(strMaxID, intLength - 7, 8))
lngID = lngID + 1
strMaxID = (Mid(strMaxID, 1, intLength - 8)) & Format(lngID, "0000000#")
Else
strMaxID = strWhereQry & "\00000001"
End If
Return strMaxID
End Function




Responses

Posted by: Neeks on: 7/19/2010 [Member] Bronze

Up
0
Down
Are you looking for this code?

public string GenerateAutoID()

{
string strQry = null;
string strWhereQry = null;
string strMaxID = null;
int intLength = 0;
long lngID = 0;
object objRet = null;

if (!string.IsNullOrEmpty(this.AcademicYear)) {
strWhereQry = this.SchoolType + "\\" + this.SchoolTransCode + "\\" + this.FormTransCode + "\\" + this.AcademicYear;
} else {
strWhereQry = this.SchoolType + "\\" + this.SchoolTransCode + "\\" + this.FormTransCode;
}
strQry = "Select Max(" + this.IDColumnName + ") from " + this.IDTableName + " where " + this.IDColumnName + " like '" + strWhereQry + "%'";
objRet = SqlHelper.ExecuteScalar(strConnection, CommandType.Text, strQry);

if (Information.IsDBNull(objRet) == false) {
strMaxID = Convert.ToString(objRet);
intLength = Strings.Len(strMaxID);
lngID = Convert.ToInt64(Strings.Mid(strMaxID, intLength - 7, 8));
lngID = lngID + 1;
strMaxID = (Strings.Mid(strMaxID, 1, intLength - 8)) + Strings.Format(lngID, "0000000#");
} else {
strMaxID = strWhereQry + "\\00000001";
}
return strMaxID;
}



Reference:
I have converted the code from-
http://converter.telerik.com/

Please notify whether you are satisfied or not?


Amar888, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vikvish on: 7/20/2010 [Member] Starter

Up
0
Down
Check the following link below:-

http://www.developerfusion.com/tools/convert/vb-to-csharp/

With regards
vik

vikram

Amar888, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response