|
I have the following linq to sql statement that I want to use in a C# 2008 application that connects to a sql server 2008 r2 database.
var varGoodTransCount = (from t in rData.Trans
join iw in rData.Ibooks on t.Ibook_ID equals iw.Ibook_ID
join ip in rData.IPacks on iw.PID equals ip.PID
where ip.TNum == SPkgID
group ip by ip.TNum into g
select new { TNum = g.Key, Frequency = g.Count() }).FirstOrDefault();
The line of code that has the problem is
If (varGoodTransCount.Frequency != null)
When I try to reference varGoodTransCount.Frequency, the error message says this is not an instance of an object.
Thus can you tell me what is wrong and how I can fix the problem?
|
|
|
|
|
Without your database data it is not possible to be specific, but I would suspect that there are no rows which match your where condition.
As a result, FirstOrDefault will return a null , and you would get the error you describe.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
I am trying to write a linq to sql statement that is having a problem. The error message says the value is not an instance of an object.
The sql similar is the following:
select COUNT (Trans_id),ip.TNum, from dbo.Trans t inner join dbo.IWbook iw on t.IWork_ID = iw.IWork_ID inner join dbo.IPack ip on ip.PID = iw.PID where ip.TNum = @parameter_value group by ip.TNum
The linq is the following:
var varGoodTransCount = (from t in rData.Trans join iw in rData.Ibooks on t.Ibook_ID equals iw.Ibook_ID join ip in rData.IPacks on iw.PID equals ip.PID where ip.TNum == SPkgID group ip by ip.TNum into g select new { TNum = g.Key, Frequency = g.Count() }).FirstOrDefault();
I am checking for if (varGoodTransCount.frequency != null) when the error is occuring.
Thus can you tell me what could be wrong?
|
|
|
|
|
Read my previous messgae:
"I would suspect that there are no rows which match your where condition.
As a result, FirstOrDefault will return a null, and you would get the error you describe."If there are no rows that match, varGoodTransCount will be null, and varGoodTransCount.frequency will throw the exception when you try to use it.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
The report works in my computer. But when I deploy to another computer, it doesn't work. It got login in prompt.
I used SqlClient to get data then bind to DataSet, then set report source.
My environment: XP + VS 2003
DataBase : MS SQL 2005 (another server)
System : Windows Form
Client PC : XP
Does anyone have any suggestions?
Thanks
|
|
|
|
|
Hi. I myself am new to Crystal Reports and giving just a pointer. Have you tried this with Windows Authentication on Sql Server (on other computer)?
This world is going to explode due to international politics, SOON.
|
|
|
|
|
Hello everyone,
I'm pretty new to this forum and have a question regarding to using C# to aggregate a csv file containing the vehicle position data into certain time interval. The format of the data is like follows:
Timestamp(s), linkNo, ......... NOx, PM10, CO2,
27000, 10, ..... 0.4, 1.2, 2.0
27000, 12, ..... 0.3, 1.4 3.0
27010, 10, ..... 0.4, 1.2, 2.0
27010, 12, ..... 0.3, 1.4 3.0
27020, 10, ..... 0.4, 1.2, 2.0
27020, 12, ..... 0.3, 1.4 3.0
.......
</pre>
The problem i'm trying to solve is to aggregate these data into 5 minutes interval and sum up all the pollutants (i.e. NOX, PM1O and CO2) on each link, the final results i want to get is:
<pre lang="xml">
TimeSlot, linkNo, ......... SumNOx, SumPM10, SumCO2,
27000, 10, ..... 1.2, 3.6, 6.0
27000, 12, ..... 0.3, 4.2 9.0
27300, 10, ..... xx, xx, xx
27600, 12, ..... xx, xx, xx
.......
<pre lang="c#"><
i have written some code to get the sum of these pollutants based on links, but i don't know how to aggregate them into each 5 minutes slot. here is the code i written so far:
public partial class FormMain : Form
{
private double TimeSlotFunction(double SimulationTime)
{
double Time = 0;
for (int i = 1; i <= 24; i++)
{
double StartTime = 27000 + 300 * (i - 1);
double EndTime = StartTime + 300 * i;
if (StartTime < SimulationTime && SimulationTime <= EndTime)
{
Time = StartTime;
break;
}
}
return Time;
}
private class LinkData
{
public string LinkName;
public List<double> SimTime = new List<double>();
public List<double> timeSlot = new List<double>();
public List<double> NOx = new List<double>();
public List<double> PM10 = new List<double>();
public List<double> TotalCarbon = new List<double>();
public double SumNOx;
public double SumPM10;
public double SumTotalCarbon;
public double SumCO2;
}
foreach (string[] row in parsedData)
{
Entries.Add(row);
}
var LinkDict = new Dictionary<string, LinkData>();
for (int index = 1; index < Entries.Count; index++)
{
var row = Entries[index];
if (!LinkDict.ContainsKey(row[1]))
{
var newLink = new LinkData { LinkName = row[1] };
LinkDict.Add(row[1], newLink);
}
LinkDict[row[1]].SimTime.Add(Convert.ToDouble((row[0])));
LinkDict[row[1]].NOx.Add(Convert.ToDouble((row[19])));
LinkDict[row[1]].PM10.Add(Convert.ToDouble((row[20])));
LinkDict[row[1]].TotalCarbon.Add(Convert.ToSingle((row[21])));
}
foreach (var linkValue in LinkDict.Values)
linkValue.SumNOx = linkValue.NOx.Sum();
foreach (var linkValue in LinkDict.Values)
linkValue.SumPM10 = linkValue.PM10.Sum();
foreach (var linkValue in LinkDict.Values)
linkValue.SumTotalCarbon = linkValue.TotalCarbon.Sum();
foreach (var linkValue in LinkDict.Values)
using (var sw = new StreamWriter(@"D:\Work\C#\my project\my project\AlteredData.csv"))
{
foreach (var linkValue in LinkDict.Values)
{
var sb = new StringBuilder();
sb.Append(linkValue.LinkName);
sb.Append(",");
sb.Append(linkValue.SumNOx.ToString());
sb.Append(",");
sb.Append(linkValue.SumPM10.ToString());
sb.Append(",");
sb.Append(linkValue.SumTotalCarbon.ToString());
sb.Append(",");
sb.Append(linkValue.SumCO2.ToString());
sb.Append(",");
sw.WriteLine(sb.ToString());
}
}
}
I will be grateful if anyone can give me some advice.
cheers,
Simon
|
|
|
|
|
0
I am trying to write a linq to sql statement in a C# 2008 application that writes to a sql server 2008 database.
I am able to write the following statement where there are no syntax errors;
var fdrPkg = (from a in rData.ails where a.PID == S).
However the above editor will not let me write the select part of the statement where I add firstordefault.
Thus can you tell me what you think could be wrong and/or how I can change the linq to sql statement so that it will work for me?
|
|
|
|
|
Hi,
I made a software with several forms and a few of them have a private constructor and a public static method that is responsible for creating, initializing and showing it. Everything works fine within this application, but now I made another desktop application and I added a reference in this new application to the old one, so I could reuse all forms. Well, every form I call the constructor directly works fine (mdi forms for example), but those "special" classes with private constructors give me an error compiling in release mode, but weirdly it compiles fine in debug mode... Any ideas?
that's how i call
FrmRequestPwd.Show(true, "Solicite uma senha ao seu representante!");
public partial class FrmRequestPwd : Form
{
private FrmRequestPwd()
{
InitializeComponent();
}
public static void Show(bool isError, string msg)
{
using (FrmRequestPwd frm = new FrmRequestPwd())
{
frm.lblMsg.Text = msg;
frm.Text = isError ? "Erro" : "Aviso";
frm.txtSerialHD.Text = Kernel32Managed.GetVolumeSerial('C');
frm.ShowDialog();
}
}
private void btnOK_Click(object sender, EventArgs e)
{
Close();
}
}
Thanks!
Dirso
|
|
|
|
|
dirsow wrote: but those "special" classes with private constructors give me an error compiling in release mode, but weirdly it compiles fine in debug mode... Any ideas?
Compiles fine for me. What did the error say exactly? The message might be kinda helpful. FWIW, debug and release-mode follow the same rules, unless you have some preprocessor-directives in there. I don't see any in this code, so I'm curious about the description of the error you'd get.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
I forgot to put the error message:
Quote: Error 118 The name 'FrmRequestPwd' does not exist in the current context C:\DADOS\SuperControle\Projects\NewSuperControle\SuperControle\Softwares\Appls\SuperControle.Appls.SuperSPED\FrmSuperContabil.cs 95 21 SuperControle.Appls.SuperContabil
Thanks,
Dirso.
|
|
|
|
|
Ah, you'll need to add a reference indeed, particular to the project it is mentioning. You're using that form from another assembly, and the assembly it's contained in is not loaded on default.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Hi!
Actually I already did it. I have the reference removed and added again to be sure, but still the same error. there are at least 100 forms in the reference and I use most of them in this new application and the error occurs only with 2 of them, but the weirdst part is in debug mode it works like a charm.
|
|
|
|
|
Do your "special forms" use a different namespace? What about
TheNamespace.FrmRequestPwd.Show(true, "Solicite uma senha ao seu representante!");
|
|
|
|
|
I have already working single tier and three tier application . but i want to create a four tier application in my project solution using C# .If you know any one person this details Plz Send me solution of procedure
Thanks and Regards
S.Somu
7639245585
|
|
|
|
|
"Tiers" are not a procedural thing. It's a way of thinking. It's organizing your code for seperation of concerns based on the requirements and design of your application. You cannot learn this by looking at someone elses project.
|
|
|
|
|
Why four, and not five?
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Heck, let's be generous and let him have 6!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
can you please help me on how to display all the tables on your database on the listbox?
|
|
|
|
|
You can use SMO[^] to read (and manipulate) the structure of your database.
/ravi
|
|
|
|
|
Half the answer[^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Execute below sql query agianest your database. It will retrieve all the tables availabale in the database you are using.
SELECT DISTINCT TABLE_NAME FROM information_schema.tables order by TABLE_NAME
Bind the resulted tabel to List box item
ListItem listImet;
lst.Items.Clear();
for (int i = 0; i < dt.Rows.Count; i++)
{
string Text = dt.Rows[i]["TABLE_NAME"].ToString();
listImet = new ListItem(Text);
lst.Items.Add(listImet);
}
Paly
|
|
|
|
|
Background:
To implement localization I have derived from DisplayNameAttribute: LocalDisplayNameAttribute. When using this attribute, I only want to declare the ResourceString an a default string. The class LocalDisplayNameAttribute should find out on it's own, which class and which assembly is using it.
My idea is, to find the strings in related resources by reflection. The strings should allways be in AssemblyName.Properties.Resources.
Is it possible for a attribute class, to find out, who it is used by?
Example:
Assembly LocalizationTools.dll
namespace LocalizationTools
{
public class LocalDisplayNameAttribute : DisplayNameAttribute
{
public LocalDisplayNameAttribute(string resourceName, string defaultText)
: base(defaultText)
{
m_resourceName = resourceName;
}
public override string DisplayName
{
get
{
string name = string.Empty;
try
{
name = SomeVodoo(base.DisplayNameValue);
}
catch (Exception)
{
name = base.DisplayNameValue;
}
return name;
}
}
private string SomeVodoo(string defaultText)
{
}
}
}
Assembly SomeClasses.dll
public class Foo
{
[LocalizationTools.LocalDisplayName("DisplayName_ImportantProperty", "Important property XYZ")]
public int ImportantProperty { get; set; }
}
And in SomeClasses.Properties.Resources you can find DisplayName_ImportantProperty
PS: I have a IMO less elegant solution by declaring the type.
[LocalizationTools.LocalDisplayName(typeof(SomeClasses.Properties.Resources), "DisplayName_ImportantProperty", "Important property XYZ")]
public int ImportantProperty { get; set; }
}
Thanks in advance
Andy
|
|
|
|
|
You don't. An atribute should NOT EVER modify its behavior based on the class name it's attached to. The reason is that now your attribute functionality is directly tied to the classes it's looking for. That's bad!
If you need to modify the behavior of the attribute, then you need two or more attributes, one for each behavior.
|
|
|
|
|
Hi Dave,
thanks for your answer.
Well, I did not want to change the behaviour of the attribute. It still should show the DisplayName, Description, Category etc. I only wanted to make it a bit more "smart" regarding localization.
I was just curious, if it is possibly for an attribute to figure out to which class it is attached.
It's ok for me, since I have a solution, inspired by different approaches which can be found here at CP and out in the web.
|
|
|
|
|