How do I convert an object containing an Oracle Date to a DateTime? - asp.net

The method below uses OracleDataReader.GetValues() to stuff SQL results into an array of objects.
From that array, calling methods can convert, e.g. numbers to longs with Convert.ToInt64(row["FOO_COLUMN"]), but cannot reliably get a date. I cannot use TO_CHAR(some_date_format) because the method must work with SELECT * FROM ....
I've tried checking each column to see if it is an OracleDate via the three lines of commented code below. The contents of the if() statement in question may be incorrect, but it doesn't matter because the if() condition is never met.
I've searched but was surprised that either my search skills need some work or that no one has ever asked this question, probably the former.
public IDictionary<int, IDictionary<string, object>>
dbQuery(string sql, Dictionary<string, object> parameters = null, string connectionString = null) {
var dbResults = new Dictionary<int, IDictionary<string, Object>>();
if(connectionString == null) connectionString = this.defaultQueryConnectionString;
using(var con = new OracleConnection(connectionString)) {
using(var cmd = new OracleCommand(sql, con)) {
cmd.BindByName = true;
if(parameters != null) {
OracleParameter[] parameterArray = new OracleParameter[parameters.Count];
int parameterIndex = 0;
foreach(var parameter in parameters) {
parameterArray[parameterIndex] = new OracleParameter(parameter.Key, parameter.Value);
++parameterIndex;
}
cmd.Parameters.AddRange(parameterArray);
}
con.Open();
var reader = cmd.ExecuteReader();
int columnCount = reader.FieldCount;
object[] columns = new object[columnCount];
int rowNum = 0;
while(reader.Read()) {
reader.GetValues(columns);
var colval = new Dictionary<string, object>();
for(int columnIndex = 0; columnIndex < columnCount; ++columnIndex) {
//if(columns[columnIndex] is OracleDate) {
// columns[columnIndex] = Convert.ToDateTime(columns[columnIndex]);
//}
string colName = reader.GetName(columnIndex).ToUpperInvariant();
colval.Add(colName.ToUpperInvariant(), columns[columnIndex]);
}
dbResults.Add(rowNum, colval);
++rowNum;
}
}
}
return dbResults;
}

DateTime.Parse(reader[column].ToString())

The OracleDateTime object is actually an Array of bytes. Another solution (that does not use string conversion) is:
OracleDateTime oDT = (OracleDateTime) reader[column];
DateTime? myDT = null;
if(!oDT.IsNull)
myDT = new DateTime(oDT.Year, oDT.Month, oDT.Day
, oDT.Hour, oDT.Minute, oDT.Second);

Related

Inserting dynamics 365 records into SQL Server database using asp.net

I am currently working on a project to insert a list of records from a dynamics 365 website into to a SQL Server database. However when I call the class file no insert is currently made into the database.
Can someone assist me? I have placed an ellipsis at the where the code which pulls the data from crm would be as that code works fine and so what you're reading isn't as long. Let me know if it is needed.
public class ProgramPVT
{
static void Main (string[] args)
{
try
{
...
int count = 0;
int n = count;
foreach (var item in performancevt)
{
performancevt.Add(item);
}
var totalnumber = performancevt.Count;
var t = totalnumber;
var accountmanager = new string[t];
var monthlytarget = new string[t];
var forecast_ = new string[t];
var actual_ = new string[t];
var managedservices = new string[t];
var pvtpercentage_ = new string[t];
var mspercentage_ = new string[t];
SqlConnection crmdbconnection = new SqlConnection("Data Source =*****;Initial Catalog=****;User Id = ******;Password = ******;");
crmdbconnection.Open();
foreach (var performanceitem in performancevt)
{
accountmanager[n] = performanceitem.accountmanager.ToString();
monthlytarget[n] = performanceitem.monthlytarget.ToString();
forecast_[n] = performanceitem.accountmanager.ToString();
actual_[n] = performanceitem.accountmanager.ToString();
managedservices[n] = performanceitem.monthlytarget.ToString();
pvtpercentage_[n] = performanceitem.accountmanager.ToString();
mspercentage_[n] = performanceitem.accountmanager.ToString();
var i = 0;
do
{
try
{
string cmdtext = "INSERT INTO PerformanceVTarget (Account_Manager, Month_Target, Forecast, Achieved, Total_Percentage, MS_Percentage) VALUES (#Account_Manager, #Month_Target, #Forecast, #Achieved, #Total_Percentage, #MS_Percentage)";
using (var cmd = new SqlCommand(cmdtext, crmdbconnection))
{
{
cmd.Parameters.AddWithValue("#Account_Manager", accountmanager[n]);
cmd.Parameters.AddWithValue("#Month_Target", accountmanager[n]);
cmd.Parameters.AddWithValue("#Forecast", accountmanager[n]);
cmd.Parameters.AddWithValue("#Achieved", accountmanager[n]);
cmd.Parameters.AddWithValue("#Total_Percentage", accountmanager[n]);
cmd.Parameters.AddWithValue("#Account_Manager", accountmanager[n]);
}
cmd.ExecuteNonQuery();
}
}
catch (Exception fx)
{
Console.Write(fx);
Console.WriteLine("Line with ID:", n, " not inserted");
Console.WriteLine("Error - Press enter to Continue");
Console.ReadLine();
}
i++;
} while (i < t);
}
n = n + 1;
}
catch (Exception ex)
{
Console.Write(ex);
Console.WriteLine("Press Enter to Continue");
Console.ReadLine();
}
}
}
}
Table PerformanceVTarget
[ID] pk, int ,not null
[Report_ID] int, null
[Account_Manager] varchar(50) not null
[Month_Target] varchar(50) not null
[Forecast] varchar(50) not null
[Achieved] varchar(50) not null
[Total_Percentage] varchar(50) not null
[MS_Percentage] varchar(50) not null
[Team] varchar(50) null
Your code execution will never come out of this loop.
foreach (var item in performancevt)
{
performancevt.Add(item);
}
This is a deadlock or you should get an error.
Also you are not passing any value to [MS_Percentage] & Id (primary key field), both are non-null columns.

Convert data to JSON format

i have used the Newtonsoft.Json for converting data into json format.
I have write the below code:
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string DataTableToJSONWithJSONNet()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(Int32));
DataSet ds = new DataSet();
ds = cls.ReturnDataSet("Get_data",
new SqlParameter("#Yourid", "5"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dt.Rows.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["id"].ToString()));
}
string JSONString = string.Empty;
JSONString = "{" + "''mydata''"+":" + JsonConvert.SerializeObject(dt) + "}";
return JSONString;
}
So it gives me the below output:
But i want the output like :
{"mydata":[{"id":125},{"id":137},{"id":249},{"id":201},{"id":124},
{"id":173},{"id":160},{"id":153},{"id":146},{"id":168}]}
So how can i convert to it from xml to json. ?
I run your solution in a console application and I can clearly see the problem. If you avoid building json manually, the problem will go away. As I don't have database, I have added my data rows manually. Hope that will help.
using Newtonsoft.Json;
using System;
using System.Data;
namespace Test
{
class MyDataContainer
{
public DataTable mydata { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.Write(DataTableToJSONWithJSONNet());
Console.Read();
}
static string DataTableToJSONWithJSONNet()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(Int32));
dt.Rows.Add(1);
dt.Rows.Add(2);
MyDataContainer cont = new MyDataContainer();
cont.mydata = dt;
string JSONString = string.Empty;
JSONString = JsonConvert.SerializeObject(cont);
//to see your attempt uncomment the blow lines
//Console.Write("{" + "''mydata''"+":" + JsonConvert.SerializeObject(dt) + "}");
//Console.WriteLine();
return JSONString;
}
}
}
Looking into your codes, you are already declared that your output is type of JSON, so on the response data it will return a JSON string.
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
And you also declared that this is a ScriptMethod. My thought is you are testing your app by running your code and accessing the url of the web service - for example http://localhost/test.asmx and clicking the invoke button on your DataTableToJSONWithJSONNet method. This approach will really display JSON result enclosed on XML format. The best way to test your own code is to invoke the web service using something like jQuery Ajax or equivalent (client scripts).
You can change your code to something like this to achieve the output you are looking for:
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public MyResponse DataTableToJSONWithJSONNet()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(Int32));
DataSet ds = new DataSet();
ds = cls.ReturnDataSet("Get_data",
new SqlParameter("#Yourid", "5"));
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
dt.Rows.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["id"].ToString()));
}
MyResponse result = new MyResponse();
result.mydata = dt;
return result;
}
class MyResponse
{
private object _mydata;
public object mydata { get { return this._mydata; } set { this._mydata = value; } }
public MyResponse() { }
}

Session variable accessibility

I have a class which creates a session the following way:
Session["UserId"] = UserId; // UserId = 1
In one of the pages on Page_Load, I retrieve the session variable value this way which works fine:
if (Session["UserId"] != null){
var userid = Session["UserId"];
Welcome.Text = "Hello, " + userid;
}
Now I need to use the value of the session variable in my classes as well. I used the following method to get the session value int useridsession = Convert.ToInt32(HttpContext.Current.Session["UserId"]); but it always returns null whereas the session is correctly read using Session["UserId"]; in my code behind file.
The mentioned class:
public static DataTable ManageBookingsDataTable()
{
int useridsession = Convert.ToInt32(HttpContext.Current.Session["UserId"]);
SqlConnection con = new SqlConnection(Database.ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select bookings.id,user_id, start_date, end_date, pets.name AS 'Pet name' from bookings AS bookings left join users AS usr ON bookings.user_id=usr.id AND bookings.user_id=1 left join pets AS pets ON pets.id=bookings.pet_id WHERE bookings.user_id=#userid_session", con);
cmd.Parameters.AddWithValue("#userid_session", useridsession);
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
string id = string.Empty;
string name = string.Empty;
string startdate = string.Empty;
string enddate = string.Empty;
string full_string = string.Empty;
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
id = dt.Rows[i]["id"].ToString();
var sdate = dt.Rows[i]["start_date"];
name = dt.Rows[i]["Pet name"].ToString();
startdate = dt.Rows[i]["start_date"].ToString();
enddate = dt.Rows[i]["end_date"].ToString();
full_string = startdate + " to " + enddate + " (" + name + ")";
//CurrentBookings.Items.Add(new ListItem(full_string, id));
}
}
return dt;
}
I diagnosed the problem by adding HttpContext.Current.Session["UserId"] = 1; which proves that when I set the session in the same class, the method works.
My question is how do I access the previously created session from any class?
Session should be available for the session regardless of the class. Why don't you set and get
your session values using the same class? All your session stuff in a one place so it is more neat and tidy as well.
Local.MySession.UserId = UserId; //Set here
int myUserId = Local.MySession.UserId; //Get here
//Something like this??
namespace Local
{
//You can add all session variables to this class
public class MySession
{
public static int UserId
{
get
{
return Convert.ToInt32(HttpContext.Current.Session["userId"] ?? "0");
}
set { HttpContext.Current.Session["userId"] = value.ToString(); }
}
public static string UserEmail //For Example another session variable
{
get { return HttpContext.Current.Session["email"] ?? ""; }
set { HttpContext.Current.Session["email"] = value; }
}
}
}

I need to fetch all users email of a particular group using AD in .net 2.0

I know there are lots of methods already given in stackoverflow but in my case all of them taking too long time. I post a method which takes less time but still it is too long to implement. Please help me so that it takes less execution time. Also take consideration that i am using .net 2.0 framework.
try
{
List<string> lstEmails = new List<string>();
string filter1 = string.Format("(anr={0})", "groupname");
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = filter1;
searcher.SearchScope = SearchScope.Subtree;
searcher.PropertiesToLoad.Add("mail");
IEnumerable res = (IEnumerable)searcher.FindOne().GetDirectoryEntry().Invoke("members");
//IEnumerable<string> rest = (IEnumerable<string>)res;
if (res != null)
{
try
{
int index = 0;
foreach (IEnumerable resl in res)
{
DateTime start = DateTime.Now;
DirectoryEntry dr = new DirectoryEntry(resl);
string strEmail = null;
if (dr.Properties["mail"].Value != null)
{
strEmail = dr.Properties["mail"].Value.ToString();
Console.WriteLine(strEmail);
DateTime stop = DateTime.Now;
Console.WriteLine((stop - start).TotalMinutes.ToString());
index++;
Console.WriteLine(index.ToString());
}
if (!string.IsNullOrEmpty(strEmail))
{
// groupMemebers.Add("sam",strEmail);
}
}
}
catch { }
}
}
catch { }
This is your suggested method Daro..
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domain, "domainname" + strLDAPUserName, strLDAPPassword);
DomainController controller = DomainController.FindOne(context);
DirectoryEntry entry = new DirectoryEntry(string.Format("LDAP://{0}",controller.Domain), strLDAPUserName, strLDAPPassword, AuthenticationTypes.Secure);
List<string> userList = new List<string>();
DateTime StartTime = DateTime.Now;
using (DirectorySearcher ds = new DirectorySearcher(entry))
{
ds.PropertiesToLoad.Add("mail");
ds.PageSize = 10000;
string DistingushiedName = "CN=" + groupName + ",OU=Users,dc=CompanyName,dc=com";
ds.Filter = "(&(objectClass=user)(memberof:1.2.840.113556.1.4.1941:="+DistingushiedName+"))";
ds.SearchScope = SearchScope.Subtree;
try
{
foreach (SearchResult user in ds.FindAll())
{
try
{
userList.Add(user.Path);//.Properties["mail"][0].ToString());
}
catch (Exception E)
{
throw new Exception(E.Message);
}
}
}
catch(Exception E)
{
throw new Exception(E.Message);
}
DateTime EndTime = DateTime.Now;
TimeSpan Dif = EndTime.Subtract(StartTime);
}
Here is your solution:-
string[] email = new string[0];
DirectoryEntry entry = new DirectoryEntry("LDAP://OU=Users,dc=me,dc=com", username, password);
string groupName = "GroupName";//Group NAme
DirectorySearcher groupSearch = new DirectorySearcher(entry);
groupSearch.Filter = "(SAMAccountName=" + groupName + ")";
groupSearch.PropertiesToLoad.Add("member");
SearchResult groupResult = groupSearch.FindOne(); // getting the members who belongs to the concern groupname
if (groupResult != null)
{
email = new string[groupResult.Properties["member"].Count]; //creatign an array to store all the email address
for (int iSearchLoop = 0; iSearchLoop < groupResult.Properties["member"].Count; iSearchLoop++)
{
string userName = groupResult.Properties["member"][iSearchLoop].ToString();
int index = userName.IndexOf(',');
userName = userName.Substring(0, index).Replace("CN=", "").ToString(); // the name of the user will be fetched.
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(name=" + userName + ")";
search.PropertiesToLoad.Add("mail");
SearchResult result = search.FindOne(); //finding the mail id
if (result != null)
{
email[iSearchLoop] = result.Properties["mail"][0].ToString(); //assigning the mail id to an array....
}
}
}
Hope this helps you
Easy enough (if your AD is 2003 R2 or higher):
List<string> userList = new List<string>();
DateTime StartTime = DateTime.Now;
using (DirectorySearcher ds = new DirectorySearcher(new DirectoryEntry ("GC://DC=YourDomain,DC=com")))
{
ds.PropertiesToLoad.Add("mail");
ds.PageSize = 10000;
ds.Filter = "(&(objectClass=user)(memberof:1.2.840.113556.1.4.1941:=YOUR_GROUP'S DN))";
ds.SearchScope = SearchScope.Subtree;
try
{
foreach (SearchResult user in ds.FindAll())
{
try
{
userList.Add(user.Path);//.Properties["mail"][0].ToString());
}
catch (Exception E)
{
throw new Exception(E.Message);
}
}
}
catch(Exception E)
{
throw new Exception(E.Message);
}
DateTime EndTime = DateTime.Now;
TimeSpan Dif = EndTime.Subtract(StartTime);
}
Replace YOUR_GROUP'S DN with the distiguished name of your group...
memberof:1.2.840.113556.1.4.1941:= is the "new" LDAP_MATCHING_RULE_IN_CHAIN operator, and retrieves all group members. Look here to see if your AD is ready and get more information.
Edit:
I gave you an answer, but an explanation might help further.
In general you should avoid ANR searches because they expand to large wildcard OR queries. Use them only if you have no idea which property contains the name you are searching for. They are much slower than explicit AND searches.
Secondly if you have more than one domain, you should turn off referral chasing unless you want to search through all domains until you get a hit. In this case it would be better to do a GC:// than a LDAP:// search to find the object you’re looking for, than do an LDAP search on that object. Depending on what you are looking for the GC query could well be enough
Edit 2:
Modified the code to give more error information and get the user path instead of E-Mail.
Hey this is the correct way...
try
{
List<string> ReturnArray = new List<string>();
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domainName, domainName + "\\" + UserName, Password);
DomainController controller = DomainController.FindOne(context);
string LDAPAddress = string.Format("LDAP://{0}", controller.Domain);
DirectoryEntry deDirEntry = new DirectoryEntry(LDAPAddress, UserName, Password);
deDirEntry.AuthenticationType = AuthenticationTypes.Secure;
DirectorySearcher mySearcher = new DirectorySearcher(deDirEntry);
mySearcher.PropertiesToLoad.Add("distinguishedName");
string sFilter = String.Format("(&(objectcategory=group)(cn=" + GroupName + "))");
mySearcher.Filter = sFilter;
mySearcher.Sort.Direction = SortDirection.Ascending;
mySearcher.Sort.PropertyName = "cn";
SearchResult result;
DirectoryEntry ResultEntry;
result = mySearcher.FindOne();
ResultEntry = result.GetDirectoryEntry();
GroupName = ResultEntry.Properties["distinguishedName"].Value.ToString();
mySearcher = new DirectorySearcher(deDirEntry);
mySearcher.PropertiesToLoad.Add("cn");
mySearcher.PropertiesToLoad.Add("mail");
sFilter = String.Format("(&(objectClass=person)(memberOf={0}))", GroupName);
mySearcher.Filter = sFilter;
mySearcher.Sort.Direction = SortDirection.Ascending;
mySearcher.Sort.PropertyName = "cn";
SearchResultCollection results;
results = mySearcher.FindAll();
foreach (SearchResult resEnt in results)
{
ResultPropertyCollection propcoll = resEnt.Properties;
foreach (string key in propcoll.PropertyNames)
{
if (key == "mail")
{
foreach (object values in propcoll[key])
{
if (!String.IsNullOrEmpty(values.ToString()))
{
ReturnArray.Add(values.ToString());
Console.WriteLine(values.ToString());
}
}
}
}
}
return ReturnArray;
}
catch
{
return null;
}
Thanks everyone for your valuable suggesstion

Manually converting result dataset to JSON

I have DataReader that holds the results from a stored procedure caal. The results consist of two fields ...
UserID
UserName
Normally I bind these results to an ASP.NET dropdownlist control ...
ddlUserList.DataSource = rdr // rdr is the DataReader
ddlUserList.DataTextField = "UserName"
ddlUserList.DataValueField = "UserID"
ddlUserList.DataBind()
However I am now trying to accomplish the same thing using jQuery AJAX. What I am stuck on is how to manually convert the dataset held in the DataReader to JSON. How are multiples values separated? Does this look correct?
{{"UserID":1, "UserName":"Bob"}, {"UserID":2, "UserName":"Sally"},{"UserID":3, "UserName":"Fred"}}
I realize there are libraries out there such as JSON.NET to handle the serialization but I am in the learning stage now and want to make sure I understand everything from the bottom up.
Was wondering if you have tried using System.Web.Script.Serialization.JavaScriptSerializer library?
You can look at Rick Stahl's blog on this:
http://www.west-wind.com/weblog/posts/737584.aspx
Or you could also do something like create a method that will pull out data from the datareader and place it in a list of objects. (See code below). These list of object will be serialized using the JavaScriptSerializer library.
Hope this helps!
public class User
{
public int UserId { get; set; }
public string UserName { get; set;}
}
public class DataLayer
{
public string GetUsers(string connString)
{
string result = null;
List<User> users = null;
// get data using SqlReader
using(var conn = new SqlConnection(connString))
{
using(var cmd = new SqlCommand{ Connection = conn, CommandText = "SELECT * FROM Users", CommandType = CommandType.Text })
{
conn.Open();
var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if(!reader.HasRows)
return null;
//convert data reader to a list of user objects
users = (List<User>)ConvertToList<User>(ref reader);
conn.Close();
}
}
//convert list of objects in list to json objects
var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
result = jsonSerializer.Serialize(users);
return result;
}
public static IList<T> ConvertToList<T>(ref SqlDataReader reader)
{
IList<T> result = null;
if (reader.IsClosed)
return result;
result = new List<T>();
T item = default(T);
while (reader.Read())
{
//create item instance
item = (T)Activator.CreateInstance<T>();
//get class property members
var propertyItems = item.GetType().GetProperties();
//populate class property members with data from data reader
for (int ctr = 0; ctr < reader.FieldCount; ctr++)
{
if(reader.GetName(ctr) == propertyItems[ctr].Name)
propertyItems[ctr].SetValue(item, UtilsHelper.GetValue<string>(reader[ctr]), null);
}
//add item to list
result.Add(item);
}
reader.Close();
reader.Dispose();
return result;
}
}

Resources