By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,490 Members | 3,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,490 IT Pros & Developers. It's quick & easy.

please help, changing the value problem

P: 3
Hello guys, I have this kind of problem:
first, the textBox tbISBN has a value that is read from the text file, but then after pressing the "Clear" button, it should be realized, and when I enter the new text into the textBox it should get the new text that I entered, but it keeps it's first value, even after the "Clear" button event.
Here is the code.

Expand|Select|Wrap|Line Numbers
  1. public partial class _Default : System.Web.UI.Page 
  2. {
  3.    private Book selectedBook;
  4.    public string strISBN;
  5.    public string strCurrent;
  6.    public string[] strFields;
  7.    public char[] chrDelimiter = { ',' };
  8.    public SortedList slBook;
  9.    public StreamReader srdCurrent;
  10.  
  11.     public string content;
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if (!IsPostBack)
  15.         {
  16.             SortedList slBooks = new SortedList();
  17.  
  18.             StreamReader srdCurrent = new StreamReader(Request.MapPath(@"App_Data\Books.txt"));
  19.  
  20.             strCurrent = srdCurrent.ReadLine();
  21.  
  22.             while (strCurrent != null)
  23.             {
  24.                 Book recNew = new Book();
  25.                 strFields = strCurrent.Split(chrDelimiter);
  26.  
  27.                 recNew.Title = strFields[0];
  28.                 recNew.ISBN = strFields[1];
  29.                 recNew.Author = strFields[2];
  30.                 recNew.Publisher = strFields[3];
  31.                 recNew.UnitPrice = System.Convert.ToDecimal(strFields[4]);
  32.  
  33.                 slBooks.Add(recNew.Title, recNew);
  34.                 strCurrent = srdCurrent.ReadLine();
  35.  
  36.                 // Add the book title to the drop down list
  37.                 ddlBooks.Items.Add(recNew.Title);
  38.             }
  39.  
  40.             // Close the file when reading is complete.
  41.             srdCurrent.Close();
  42.  
  43.             // Add the sorted list to this session 
  44.             Session.Add("Book", slBooks);
  45.  
  46.         }
  47.  
  48.         selectedBook = GetSelectedBook();
  49.         if (selectedBook != null)
  50.         {
  51.            tbTitle.Text = selectedBook.Title;
  52.             tbISBN.Text = selectedBook.ISBN;
  53.             tbAuthor.Text = selectedBook.Author;
  54.             tbPublisher.Text = selectedBook.Publisher;
  55.             tbPrice.Text = selectedBook.UnitPrice.ToString("c");
  56.             imgBook.ImageUrl = "Images/Books/" + selectedBook.ISBN + ".gif";
  57.         }
  58.     }
  59.     private Book GetSelectedBook()
  60.     {
  61.         SortedList slBooks = (SortedList)Session["Book"];
  62.         if (slBooks == null)
  63.             return null;
  64.  
  65.         string strTitle = ddlBooks.SelectedValue;
  66.         return (Book)slBooks[strTitle];
  67.     }
  68.  
  69.  
  70.     //***********************************************************************************
  71.     public void btnClear_Click(object sender, EventArgs e)
  72.     {
  73.         /*selectedBook.Title = "";
  74.         selectedBook.ISBN = "";
  75.         selectedBook.Author = "";
  76.         selectedBook.Publisher = "";
  77.         selectedBook.UnitPrice = 0.00m;*/
  78.         StreamWriter delete = new StreamWriter(Request.MapPath(@"App_Data\Boo.txt"));
  79.  
  80.         tbTitle.Text = "";
  81.         tbISBN.Text = "";
  82.         tbAuthor.Text = "";
  83.         tbPublisher.Text = "";
  84.         tbPrice.Text = "";
  85.         imgBook.ImageUrl = "";
  86.         lblUploadResult.Text = "";
  87.  
  88.     }
  89.     //***********************************************************************************
  90.     protected void AddBook()
  91.     {
  92.         strISBN = tbISBN.Text;
  93.         string filePath = Server.MapPath(@"App_Data\Books.txt");
  94.  
  95.         StreamReader sr = File.OpenText(filePath);
  96.  
  97.         //StreamReader srdCurrent = new StreamReader(Request.MapPath(@"App_Data\Books.txt"));
  98.  
  99.         //StreamWriter rite = new StreamWriter(Request.MapPath(@"App_Data\Boo.txt"));
  100.         //rite.WriteLine("any string");
  101.         //rite.Close();
  102.  
  103.         try
  104.         {
  105.  
  106.             content = sr.ReadToEnd();
  107.             if (content.Contains(strISBN))
  108.             {
  109.                 lblUploadResult.Text = "isbn contained";
  110.                 sr.Close();
  111.             }
  112.             else
  113.             {
  114.                 StreamReader srdCurrent = new StreamReader(Request.MapPath(@"App_Data\Books.txt"));
  115.  
  116.                 StreamWriter rite = new StreamWriter(Request.MapPath(@"App_Data\Boo.txt"));
  117.                 rite.WriteLine("any string");
  118.                 rite.Close();
  119.  
  120.             }
  121.         }
  122.         catch (Exception) { }
  123.  
  124.     }
  125.  
  126.  
  127.  
  128.  
  129.     //**************************************************************************************
  130.  
  131.     protected void btnSave_Click(object sender, EventArgs e)
  132.     {
  133.         AddBook();
  134.     }
  135.  
  136.  
  137. }
  138.  
Thank you very much!
Mar 22 '07 #1
Share this Question
Share on Google+
1 Reply


dorinbogdan
Expert 100+
P: 839
Try to assign a char array (char[]) to tbISBN.Text instead of a String.
If still not working, please post or attach the required code to run this part of project.
Mar 23 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.