|
Interesting, I didnt notice passing your ICollection implentation as a parameter to the constructor, uses that. I will keep this in mind
However, I still cannot see why it should take so much longer. Maybe fixing the Capacity to a HUGE initial value could solve it, the only thing I can think of is collision checking, so the hash is more spread, that will require more memory too.
top secret xacc-ide 0.0.1
|
|
|
|
|
When you pass an ICollection to the ArrayList ctor, the Capacity is set to the number of elements in the ICollection using the Count , yes.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
How to cast from type 'DBNull' to type 'integer'
|
|
|
|
|
IamADotNetGuy wrote:
How to cast from type 'DBNull' to type 'integer'
Why? Null means the absence of a value. What integer value would you cast it to?
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
Hi
sorry bit correction
One of the table in database is accepting Integer value,
But sometimes i want to pass null value to this column.
Please let me know on this
|
|
|
|
|
Ah! Okay that is easier:
If you are passing values in as parameters it is something like this:
command.Parameters.Add("@MyInteger", DBNull.Value);
If you are using some other means of inserting data and this doesn't make sense, let me know and I'll see if I can cook up an example for you.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
to pass a null value to a DB you must specify the value NULL in the insert string...
INSERT INTO MyTable (MyColumn) VALUES (NULL)
now if you want to retrieve NULL's as a 0 value from the column you could do the following in SQL:
SELECT ISNULL(MyColumn, 0) FROM MyTable
this will return the value of 0 instead of NULL
of course this is assuming you are writing the SQL string if not, then I'd go with the other solutions
|
|
|
|
|
The best I can think of is something like this:
object fieldValue = reader.GetValue(columnIndex);
if (fieldValue == System.DBNull.Value)
fieldValue = 0;
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
Sorry. No can do...
DbNull represents an unknown value, and an int has no means of representing that.
One way around this can be to define a specific integer value as the value of unknown/not set/DbNull. For instance use -1 for this if valid values for your integer always are positive.
object obj = some_value_thats_either_an_int_or_dbnull;
int i = obj is DbNull.Value ? -1 : (int)obj;
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
|
How can i insert null value to a column of number type??
|
|
|
|
|
|
I am new to C# but have plenty of programming knowledge in C++ and Java.
I have a question on my logic for a program I am doing.
What I am doing is reading in a few tables from an Access DB and binding them to a form textbox fields. The form comes up with "company" infomation, and then in a subform is the contacts for that specific company. The company information is in one table with a foreign key to the contact table.
Now I can bring up all the company info and all the contact info right now. But I want the subform just to show the contacts just for that specific company. Is there a "filter" or something that I can run against my contact DataSet to show the appropiate rows, and hide the others?
I know this is pretty vague, but I just need the logic on how to do something like this.
_Jacob
|
|
|
|
|
What are you using for a control to display the contacts?
Yes, there is a way to filter results. See the DataView class, which you create over a DataTable (or modify DataTable.DefaultView ). But you may not have to.
If you use a DataGrid to display the contacts, you set the DataSource property to the DataSet , and set the DataMember to the "table.relationshipName", where "table" is your table name and "relationshipName" is your company<-contact relationship name. I know this works with two DataGrid s, and it should work with bound TextBox es since they use the same currency manager.
When using two DataGrid s, they both have the DataSource property set to the same DataSet . The master grid sets the DataMember to the table name only. The detail grid does what I outlined above. When you select a record in the master grid, the current item is used to automatically filter (via the DataRelation ) the detail grid. Since this all ties back to the CurrencyManager , a bound TextBox should work, too.
Here's to hoping!
Let me know. I don't have time to throw something together right now and have never had a need to try it this particular way.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Ok I will include some code.
I am pretty much doing what you said, - the DateView. I have to look up how to use that.
Heres what I have so far for an idea...
myConn.Open() ;
//Create DataSet myCompanyDataSet
OleDbDataAdapter myCompanyAdapter = new OleDbDataAdapter(strCompanyCom, myConn);
OleDbCommandBuilder myCompanyBuilder = new OleDbCommandBuilder(myCompanyAdapter);
myCompanyAdapter.FillSchema(myCompanyDataSet, SchemaType.Source);
DataTable CompanyTable = myCompanyDataSet.Tables["Table"];
CompanyTable.TableName = "tblCompany";
myCompanyAdapter.Fill(myCompanyDataSet,"tblCompany");
//Create DataSet myContactDataSet
OleDbDataAdapter myContactAdapter = new OleDbDataAdapter(strContactCom, myConn);
OleDbCommandBuilder myContactBuilder = new OleDbCommandBuilder(myContactAdapter);
myContactAdapter.FillSchema(myCompanyDataSet, SchemaType.Source);
CompanyTable = myCompanyDataSet.Tables["Table"];
CompanyTable.TableName = "tblContact";
myContactAdapter.Fill(myCompanyDataSet,"tblContact");
//Setup Relationship
myCompanyDataSet.Relations.Add(new DataRelation("ParentChild",
myCompanyDataSet.Tables["tblCompany"].Columns["COMPANYID"],
myCompanyDataSet.Tables["tblContact"].Columns["CompanyID"]));
---------------------------------
Thats what I have to for the connection and relationship.
my dataGrid is hooked up like this:
dataGrid1.DataSource = myCompanyDataSet;
dataGrid1.DataMember = "tblContact";
----------------------------------
Right now it still just prints all the contacts on the table no matter what record i am on.
Do you have any comments on this?
|
|
|
|
|
Ok I have it now being sorted:
I had to have the binding look like this:
dataGrid1.DataSource = myCompanyDataSet;
dataGrid1.DataMember = "tblCompany.ParentChild";
I just realized you have to use the master table then the relationship to pull out the filtered contacts.
My last Question is that when I am pressing my next/previous buttons and the text fields are updating with the right info but the Contact subform still shows the same information. ie its not being updated. I am thinking that the binding is not set correctly?? or maybe its not recomparing the table right to find the current company and contacts??
Thanks for the help.
Anyone have any hints?
|
|
|
|
|
I everyone... I need your help...
I am new to C# but i have good base in C++... its all look the same anyway...
Ok so, here the situation. I want to create a template container class. In c#, I found that is call a generic. So i use the 'Object' parameter for every function who need to works on the object that is "contained". My container is first design to use numerical object (int, float double), but to make good programmation i want it to be more flexible.
So here's my problem. I got some method that use mathematical operand like '+','-' and '*' on object thats is in my Arraylist. At compilation time i got the error:
Operator '+' cannot be applied to operands of type 'object' and 'object'
Yeah, I know, but there is no object in...yet....
Why is he bothering me with that. if a put a "int" in the list the '+' is define...
So, does anyone know a way to make a generic container that can use arithmetic operand on object.
Like a good old template class...
here a code sample to show what i mean:
the function that cause problem:
////////////////////
public void SourceAddSourceHandler(object S,OnNewDataEventArgs Args)
{
if(mLeftS!=null && mRightS!= null)
{
for(int i=this.Count;i
|
|
|
|
|
An object in C# is not the same thing as a template in C++. The current version of C# does not have generics nor nothing similar to templates.
You're right, you can overcome this limitation by using interfaces and/or the object data type. But you should think the object data type in the same way you use the 'void *' in C++: most of the time you'll need to cast it to the appropriate type to do something useful.
HTH...
Yes, even I am blogging now!
|
|
|
|
|
And to add to what Daniel said, that casting can be expensive, especially if your casting a struct to an object (boxing). An object is created and its members are filled with the members of the struct, then it is returned.
Generics in .NET (not just C#, mind you) will eliminate this problem since the object will actually be stored as the actual object type (unless it's a child class of an abstract type or interface, but that's besides the point) and no casting or boxing/unboxing is required.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I created a web control to display html files on my form. My idea was to click space bar and to go to next file (html, picture or movie) in the folder, but my form looses focus and doesn't pick up the keypress event. I turned on key preview and made a button which should get the focus after the web control displays what it is suppose to diplay. The problem I am having is that when I am playing a movie or a flash file, it won't transfer the focus to the button or to the form. But, if I make a messagebox right after I give button the focus, then the focus is set to the button. How can I fix this?
|
|
|
|
|
I'm trying to print out a datagrid. I was able to print out about half of the datagrid but the second half did not print. How do I print out the entire datagrid? This is the code that I used.
private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PaintEventArgs myPaintArgs = new PaintEventArgs (e.Graphics, new Rectangle(new Point(0, 0), this.Size));
this.InvokePaint(dataGrid1, myPaintArgs);
}
private void printButton_Click(object sender, System.EventArgs e)
{
printDocument1.Print();
}
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
printDialog1.ShowDialog();
}
|
|
|
|
|
What do you mean "the other half didn't print"? Please be specific.
Note that the method you're using will only work for controls that GDI+ draws. Most of the Windows Forms controls, for example, rely on the GDI drawing in the common controls they encapsulate (most of the .NET controls are just wrappers to existing window classes) so the controls won't draw in their entirety.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
The datagrid is wide enough to fill two pages. Only the first page prints out. I need to find out how to print the entire thing(2 pages).
|
|
|
|
|
You need to use the dimensions for the PrintDocument to calculate the viewport on the DataGrid . There's nothing to help you, really, you just have to perform the calculations yourself.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am re-writing an old financial application of mine using ASP.NET. At the same time, I am trying to break into programming using OOP. Below is a segment of my application that I wanted to be critiqued by more experienced developers. Did I implement OOP concepts correctly? Am I wasteful with resources? Any other suggestions? Thank you.
The underlying database table looks like this:
create table Positions
(
SystemId int primary key identity(1,1) not null,
Name varchar(25) not null ,
)
There are two classes:
- Portfolio class which right now only contains properties for my business object;
- PositionsDb class which is responsible for database operations of the application.
namespace Positions
{
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
// Portfolio class.
public class Portfolio
{
private int systemId;
private string name;
public Portfolio() { ; }
public int SystemId
{
get { return systemId; }
set { systemId = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
// PositionsDb class. Handles database operations of the application.
public class PositionsDb
{
private SqlConnection connection;
public PositionsDb()
{
;
}
~PositionsDb()
{
connection.Close();
}
public void ConnectToDb()
{
connection = new SqlConnection("Data Source=localhost;Initial Catalog=Positions;User Id=PositionsUser;Password=whatever");
connection.Open();
}
public void AddPortfolio(Portfolio portfolio)
{
// Adds a new portfolio to the database
SqlCommand myCommand;
myCommand = new SqlCommand("AddPortfolio", connection);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 25));
myCommand.Parameters["@Name"].Value = portfolio.Name;
myCommand.ExecuteNonQuery();
}
public ArrayList GetAllPortfolios()
{
// Gets all portfolios from the database
ArrayList myArrayList = new ArrayList();
Portfolio tempPortfolio;
SqlCommand myCommand;
myCommand = new SqlCommand("GetAllPortfolios", connection);
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataReader myReader;
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
tempPortfolio = new Portfolio();
tempPortfolio.SystemId = myReader.GetInt32(0);
tempPortfolio.Name = myReader.GetString(1);
myArrayList.Add(tempPortfolio);
}
myReader.Close();
return myArrayList;
}
}
}
|
|
|
|
|
Although not a critique about your OOP design, which is, BTW, very hard to do without a complete model: a single class can hardly be called "OOP".
I'd like to point you to some scalability factors:
1. ADO.NET implements connection pooling. So, don't get a connection and keep it on a class field. A connection should always be opened as late as possible and "closed" (actually, returned to the pool) as late as possible.
2. C# doesn't have destructors. The syntax you used, ~PositionsDB() is for finalizers, which is a very different thing. Read more about the IDisposable interface (often referred as IDisposable pattern) or you'll kill your scalability.
3. SqlCommand, SqlDataReader and SqlConnection are IDisposable objects: you should either call Dispose on them, or, better, use the "using" statement. As a rule of thumb, for every "new" or method that returns a reference, it's the caller responsibility to call Dispose when done. Even in a situation of an error, so if you can't use the "using" statement, put the Dispose on a try/finally block.
Yes, even I am blogging now!
|
|
|
|