Solved

reading from a file

Posted on 2006-06-19
7
260 Views
Last Modified: 2010-04-16
hi there , i have a very a problem when i read from a file to my App
i am on WinForm c# .

this is the code that read the file :

sqlConnection1.Close();
                  IDbCommand cmdGetPath = sqlConnection1.CreateCommand();

                  cmdGetPath.Parameters.Clear();

                  cmdGetPath.CommandText="Select @FilePath=FileToReadPath from Bakara";
                  SqlParameter path = new SqlParameter("@FilePath",SqlDbType.VarChar,50);
                  path.Direction=ParameterDirection.Output;


                  cmdGetPath.Parameters.Add(path);

                  sqlConnection1.Open();
                  cmdGetPath.ExecuteNonQuery();
                  sqlConnection1.Close();

                  string FilePath=path.Value.ToString();
                  
                  StreamReader sr = new StreamReader(FilePath);
      
                  string input = sr.ReadToEnd();
                  sr.Close();
                  Regex re = new Regex(@"(?<ProdKey>[\d-]+)\s+(?<Quant>-\d+)",
                        RegexOptions.Compiled);
                  MatchCollection mc;
                  
                  comboBox1.Text="drawing";

                  mc = re.Matches(input);
                  foreach( Match m in mc )
                  {
                        // Get the information from the Regex object
                        string ProdKey = m.Groups["ProdKey"].Value;
                        
                        NewConn.ConnectionString=BizLog.Conn.ConnString();
                        IDbCommand CmdRunOver=NewConn.CreateCommand();
                        CmdRunOver.CommandText="SELECT ProRunID,ProKey from Products";
                        NewConn.Open();
                        IDataReader reader = CmdRunOver.ExecuteReader();
                        while (reader.Read())
                        {
                              if (ProdKey==reader["ProKey"].ToString())
                              {
                                    int QuantitySold = Convert.ToInt32(m.Groups["Quant"].Value);
                                    tbCodeParit.Text=ProdKey.ToString();
                                    tbKamot.Text=QuantitySold.ToString();
                                    tbAsmhata.Text="999";
                                    try
                                    {
                                          button1.PerformClick();
                                    }
                                    catch
                                    {
                                    }
                              }

                              }
                        NewConn.Close();
                        reader.Close();
                        }
                  this.tbAsmhata.Clear();
                  this.tbKamot.Clear();
                  this.tbCodeParit.Clear();
                  this.lblPname.Text="";
                                    
                  string Dir=System.IO.Path.GetDirectoryName(FilePath);
                  DirectoryInfo dirInfo = new DirectoryInfo(Dir);
                  DateTime dtYez = new DateTime();
                  foreach ( FileInfo f in dirInfo.GetFiles() )
                  {
                        if( f.Extension.Equals(".DOC") && (f.Name.Equals("PRESTO.DOC") ) )
                        {
                              dtYez = DateTime.Parse(f.CreationTime.ToString());
                              f.Delete();
                              statusBar1.Text="File is del !!!";

                        }
                  }
                  Bt.klitatTenoa(dtYez,DateTime.Now);

                              
            }
this is the file txt :


                                                                 -2          422
                                                                 -4          425
                                                                 -5          544
                                                                 -4          545
                                                                 -1          566
                                                                 -5          574
                                                                 -1          593
                                                                 -1          596
                                                                 -2          652
                                                                 -2          679
                                                                 -2          681
                                                                 -1          709
                                                                 -3          713
                                                                 -2          715
                                                                 -3          720
                                                                 -2          754
                                                                 -1          813
                                                                 -5          832
                                                                -79         3022
                                                                -60         3029
                                                                -30         3030
                                                               -115         3100
                                                                -56         3110
                                                                -30         3117
                                                               -123         3120
                                                                -77         3125
                                                               -227         3127
                                                               -202         3128
                                                               -103         3140
                                                                -82         3141
                                                                -70         3142
                                                                -49         3144
                                                                -75         3146
                                                               -107         3150
                                                                -75         3151
                                                                -75         3205
                                                                -45         3222
                                                                  1         3223
                                                                -50         3231
                                                                -50         3232
                                                               -107         3233
                                                               -227         3236
                                                                -75         3305


now when he read the ProductsKey and quantity the reading is worng he read the first ProKey like 422 and the second quant like -4 i dont know why he dose it is anyone here can help me to solve that problem that i have ??

i need to read 422 and -2 like the first Products row in the file
0
Comment
Question by:Tech_Men
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Assisted Solution

by:pgloor
pgloor earned 150 total points
ID: 16933568
I'm not very good with regular expressions but I think you just need to switch the <ProdKey> part with the <Quant> part in your regex rule. Make sure to switch anthing inside the open and closing brackets ().

Old line of code:
Regex re = new Regex(@"(?<ProdKey>[\d-]+)\s+(?<Quant>-\d+)", RegexOptions.Compiled);

New line of code:
Regex re = new Regex(@"(?<Quant>-\d+)\s+(?<ProdKey>[\d-]+)", RegexOptions.Compiled);

However, while this should work for any quantity with a nagative integer, I'm not sure this works for positive integers as well, so carefully check for  1 with 3223 in your example.

Peter
0
 

Author Comment

by:Tech_Men
ID: 16933612
u are write its seems to be good now eccsept to this line 3223 he skip over

how can i get this line 2 ?

i must to get the Postive number to the APP .
0
 
LVL 3

Assisted Solution

by:pg_india
pg_india earned 25 total points
ID: 16933673
The problem is with the regular expression.

Use this regular expression:

Regex re = new Regex(@"(?<ProdKey>-\d+)\s+(?<Quant>[\d-]+)",RegexOptions.Compiled);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Tech_Men
ID: 16933694
soory but its not working its dosent read any :-(
0
 

Author Comment

by:Tech_Men
ID: 16933731
this line is working good :

Regex re = new Regex(@"(?<Quant>-\d+)\s+(?<ProdKey>[\d-]+)", RegexOptions.Compiled);

but its dosent read to row whit postive number like             1         3223

how can i read pos an neg numbers in this Reg Line?
0
 
LVL 3

Accepted Solution

by:
Bowmanje earned 325 total points
ID: 16934310
Tech Men,

Try:

Regex re = new Regex(@"(?<Quant>-\d+|\d+)\s+(?<ProdKey>[\d-]+)", RegexOptions.Compiled);

It will match your problem line, but it will also match any positive number for the first column (is this a problem?).

HTH
-Josh
0
 

Author Comment

by:Tech_Men
ID: 16934535
i dont think that thie will be the problem .... its working great ..  i love u all ... thanks !!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 46
Can I command line build a C# application? 6 54
IEnumerable<T> to a List<T> 8 53
Need help with another query 10 39
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question