|
Give this a try:
ListViewItem lst1 = new ListViewItem(str, 0);
listView1.Items.Add(lst1);
modified 27-May-14 5:11am.
|
|
|
|
|
Hi, I have a program that opens an option form. For the options someone can basically list prices for various objects. I need to know how these prices can be remembered even after the program is shut down. I don't want to keep having to set the prices every time I run the program. Right now, I have a class which takes the prices and multiplies that by the quantity of an item. In that class I have static decimals. In the option form's button click I save a value to the class's static variables. Then I call those from the main form.
Thanks for your help!
|
|
|
|
|
I hear there's this new thing called a database...
(If the number of "objects" is small and steady, perhaps an XML file will suffice.)
|
|
|
|
|
You can store it in any storage location like DataBase, TextFile . When program is restarting read the content from the storage location.
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
Yeah, I pretty much feel like an idiot. I use a database for my asp.net apps, but for some reason it never occurred to me to use it for a windows form app. Anyway, thanks for the answer.
|
|
|
|
|
Ok so you have just added a new definition to not "thinking outside the box" - note the joke icon, this made me chuckle.
|
|
|
|
|
If those options are global or per user (application user), the best place to store them is a database; if the options are per machine, store them in a custom xml file.
|
|
|
|
|
If your list is something that can be thought of as application settings , put it there. The application settings are easy to manage.
Try to use but not abuse data bases (not even xml file dbs)
|
|
|
|
|
Hi, u can use resource file, as they are part of application and not stored outside of it. u can access them using stream when required, conditionally prices dont change often.if u use this u dont need DB and not even shared variables.
Avinash S. Godse
|
|
|
|
|
hello all.
I want to validate category name before insert all information about category into database.
The following condition is the way i want to compare:
Ex: ------------------------------------------------------------------------
| name in TblCategory | name in Text box | comparative result |
------------------------------------------------------------------------
| white bag | whitebag | existing |
------------------------------------------------------------------------
| whitebag | white bag | existing |
------------------------------------------------------------------------
| White Bag | white bag | existing |
------------------------------------------------------------------------
I use this following function in sql database:
BEGIN
IF EXISTS(SELECT TOP 1 *
FROM TblCategory c
WHERE (c.c_name=@name OR c.c_name=REPLACE(@name,' ',''))
AND c.c_id <> @id
)
SET @result=1
ELSE
SET @result =0
END
this give me bad result if TblCategory (white bag) and TextBox to compare has word(whitbag)...this can't compare to me
|
|
|
|
|
How does this relates to C#?
50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
|
|
|
|
|
You can use a soundex search but it will not be a satisfactory solution. If the idiot user types whitbag then the user is an idiot (or a lousy typist), you are almost certainly not going to be able to catch all typos no matter what you try.
Another idea may be to use a soundex search and let the user select an existing category or force a new category.
|
|
|
|
|
Use this:
WHERE (REPLACE(c.c_name,' ','')=REPLACE(@name,' ',''))
|
|
|
|
|
REPLACE(c.c_name,' ','') this method is replace space of the field name(c.c_name) to select not replace resutl
can you give me any ideas?
|
|
|
|
|
I cannot understand what you are trying to say. Can you be more clear ?
|
|
|
|
|
ok, this is my original question:
I want to validate category name before insert all information about category into database.
The following condition is the way that i want to compare:
Ex: ------------------------------------------------------------------------
| name in TblCategory | name in Text box | comparative result |
------------------------------------------------------------------------
| white bag-----------| whitebag---------|---existing---------|
------------------------------------------------------------------------
| whitebag----------- | white bag------- |----existing--------|
------------------------------------------------------------------------
| White Bag-----------|--white bag ------|---- existing ------|
------------------------------------------------------------------------
I use this following function in sql database:
BEGIN
IF EXISTS(SELECT TOP 1 *
FROM TblCategory c
WHERE (c.c_name=@name OR c.c_name=REPLACE(@name,' ',''))
AND c.c_id <> @id
)
SET @result=1
ELSE
SET @result =0
END
this give me bad result if TblCategory (white bag) and TextBox to compare has word(whitbag)...this can't compare to me
then you you tell me to do this :
WHERE (REPLACE(c.c_name,' ','')=REPLACE(@name,' ',''))
but this replace is only replace my field name(c.c_name) but not give me validation to insert category infomation
|
|
|
|
|
So, you want to match 'white bag' and whitbag' ? Use SOUNDEX function in T-SQL.
WHERE (SOUNDEX(REPLACE(c.c_name,' ',''))=SOUNDEX(REPLACE(@name,' ','')))
|
|
|
|
|
Thanks for your help, sounddex give me good result in my project. but if the name to compare is 'Cat' and 'Catty' or
'book' and 'book2' or 'book3' soundex give the bad result (duplicate name in sound).
Now i can solve this problem by create function bellow:
1/funtion name_compare
USE [MagicStore]
GO
/****** Object: UserDefinedFunction [dbo].[fn_name_compare] Script Date: 12/01/2009 09:06:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fn_name_compare]
(
@str1 NVARCHAR(MAX),
@str2 NVARCHAR(MAX)
)
RETURNS BIT
AS
BEGIN
DECLARE @result BIT
IF ( REPLACE(@str1,' ','')=REPLACE(@str2,' ',''))
SET @result =1
ELSE
SET @result=0
RETURN @result
END
2/Call function name compare to validate existing name:
USE [MagicStore]
GO
/****** Object: UserDefinedFunction [dbo].[fn_exist_name] Script Date: 12/01/2009 09:08:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fn_exist_name]
(
@option INT,
@id BIGINT,
@name NVARCHAR(MAX)
)
RETURNS BIT
BEGIN
DECLARE @result BIT
IF(@option=1)
BEGIN
IF EXISTS(SELECT TOP 1 *
FROM TblCategory c
WHERE 1=dbo.fn_name_compare(c.c_name,@name)
AND c.c_id<>@id
)
SET @result=1
ELSE
SET @result =0
END
else IF(@option=2)
BEGIN
IF EXISTS(SELECT TOP 1 *
FROM TblSubCategory sc
WHERE 1=dbo.fn_name_compare(sc.sc_name,@name)
AND sc.sc_id<>@id
)
SET @result=1
ELSE
SET @result =0
END
ELSE IF(@option=3)
BEGIN
IF EXISTS(SELECT TOP 1 *
FROM TblItem i
WHERE 1=dbo.fn_name_compare(i.i_name_eng,@name)
AND i.i_Id<>@id
)
SET @result =1
ELSE
SET @result=0
END
ELSE IF (@option=4)
BEGIN
IF EXISTS(SELECT TOP 1 *
FROM TblItem i
WHERE 1=dbo.fn_name_compare(i.i_name_kh,@name)
AND i.i_Id<>@id
)
SET @result=1
ELSE
SET @result=0
END
RETURN @result
END
this give me good result with all case of name validate.
|
|
|
|
|
Thanks for your help, sounddex give me good result in my project. but if the name to compare is 'Cat' and 'Catty' or
'book' and 'book2' or 'book3' soundex give the bad result (duplicate name in sound).
Now i can solve this problem by create function bellow:
1/funtion name_compare
USE [MagicStore]
GO
/****** Object: UserDefinedFunction [dbo].[fn_name_compare] Script Date: 12/01/2009 09:06:41 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fn_name_compare]
(
@str1 NVARCHAR(MAX),
@str2 NVARCHAR(MAX)
)
RETURNS BIT
AS
BEGIN
DECLARE @result BIT
IF ( REPLACE(@str1,' ','')=REPLACE(@str2,' ',''))
SET @result =1
ELSE
SET @result=0
RETURN @result
END
2/Call function name compare to validate existing name:
USE [MagicStore]
GO
/****** Object: UserDefinedFunction [dbo].[fn_exist_name] Script Date: 12/01/2009 09:08:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fn_exist_name]
(
@option INT,
@id BIGINT,
@name NVARCHAR(MAX)
)
RETURNS BIT
BEGIN
DECLARE @result BIT
IF(@option=1)
BEGIN
IF EXISTS(SELECT TOP 1 *
FROM TblCategory c
WHERE 1=dbo.fn_name_compare(c.c_name,@name)
AND c.c_id<>@id
)
SET @result=1
ELSE
SET @result =0
END
else IF(@option=2)
BEGIN
IF EXISTS(SELECT TOP 1 *
FROM TblSubCategory sc
WHERE 1=dbo.fn_name_compare(sc.sc_name,@name)
AND sc.sc_id<>@id
)
SET @result=1
ELSE
SET @result =0
END
ELSE IF(@option=3)
BEGIN
IF EXISTS(SELECT TOP 1 *
FROM TblItem i
WHERE 1=dbo.fn_name_compare(i.i_name_eng,@name)
AND i.i_Id<>@id
)
SET @result =1
ELSE
SET @result=0
END
ELSE IF (@option=4)
BEGIN
IF EXISTS(SELECT TOP 1 *
FROM TblItem i
WHERE 1=dbo.fn_name_compare(i.i_name_kh,@name)
AND i.i_Id<>@id
)
SET @result=1
ELSE
SET @result=0
END
RETURN @result
END
this give me good result with all case of name validate.
|
|
|
|
|
I'm new to C# and am trying to access and update a Access Database. I've managed to create the database and can see the results in Access. I'm also able to open the Database and load the tables, but when I try to do an update it does not post back my changes. Below is the code with comments where things seem to fail. I do not get an exception, I just don't see the changes in the database file.
// This method seems to work fine, it loads the data from the database into the six tables.
public void openDatabase(DataSet ds, string filename)
{
// I have 6 tables in the dataset/database.
string[] names = { "Project", "Portfolios", "Equations", "Scenarios", "Reports", "Rules" };
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename;
for (int i = 0; i < names.Length; i++)
{
string strAccessSelect = "SELECT * FROM " + names[i];
try
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
return;
}
try
{
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn);
myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
myDataAdapter.Fill(ds, names[i]);
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}
}
}
// This is the method I'm having trouble with, I sent the Dataset off to another routine which changes some
// of the datarows, I can see the changes there in the dataset but update does not write them to disk.
public void updateDatabase(DataSet ds)
{
try
{
dw = new DebugWindow(); // This opens up a window to display the dataset
dw.Show();
dw.dumpDataSet(ds); // This dumps the dataset to the window, the dataset looks correct with my changes
myAccessConn.Open(); // I don't think I need to do an open as the update should do this but I'm trying all kinds of things
ds.AcceptChanges(); // Don't think I need this
// This seems to be the line that does not work. I've also tried (ds, "Rules") as well. Ideally, I would
// just pass in the dataset and all the tables would be updated.
myDataAdapter.Update(ds.Tables["Rules"]);
myAccessConn.Close();
}
catch (System.Exception ex)
{
dw.displayMsg("Update Failed" + ex.Message);
}
}
|
|
|
|
|
DataAdapters are very simple; they can only do very simple things. They are really only good for writing quick-and-dirty demoes at Microsoft launch events. Any non-trivial application will quickly become complex enough that a DataAdapter will be of no use -- especially Update.
I don't use DataAdapters and neither should you (or anyone else for that matter); they cause more trouble than they're worth. You would be much better off in the long run, learning how to access databases at a lower level.
ExecuteScalar, ExecuteNonQuery, DataAdapter.Fill, and DataAdapter.Update all use ExecuteReader (and therefore DataReaders); learn to use DataReaders.
(I think part of your problem above may be that myDataAdapter gets changed between calls to openDatabase and updateDatabase. But the whole thing should be rewritten. )
|
|
|
|
|
Thank you for your insight. I'll look into using a DataReader. When I first implemented this I actually read the .mdb in binary format and converted it line by line. It seems most books point to the DataAdapters, but as you say these seem only good for the quick and dirty. Thanks.
|
|
|
|
|
Oi Adam
Listen to what Piebald said, quick and dirty or DO NOT USE THEM. Learn about a Data Access Layer (DAL) and implement one of the many existing ones available here in the articles.
Most accomplished developers build there own, I see a lot of new devs using the Enterprise library for their DAL these days.
|
|
|
|
|
You can listen to the other peoples' advice to use ExecuteNonQuery(), etc. However, I am going to just talk about your piece of code.
The adapter maintains four different query commands internally. One of them is the update query you implicitly called when you call myDataAdapter.Update(...). However, in order for it to work, the adapter has to know how to correctly construct the update query internally. What you need to make this program to work is to add the following line to tell the adapter how to construct the query internally:
OleDbCommandBuilder builder=new OleDbCommandBuilder(myDataAdapter);
Add the above line right after the line:
myDataAdapter = new OleDbDataAdapter(myAccessCommand);
After that, your Update(...) call should work.
|
|
|
|
|
Thanks dxlee, I gave this a try but update still does not seem to write to the disk. I tried to cut things down to one table as I think flipping between them is causing problems. I'll look more into the OleDBCommandBuilder. Thanks.
|
|
|
|