Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 9 của 9
  1. #1
    Cảm ơn bác zkday2686 nhiều! bác Reply rất nhanh chóng!
    Em thay code bác đưa vào đoạn code của em, thì nó hết lỗi đường dẫn, nhưng nó lại báo 1 lỗi khác (em sẽ kèm file ảnh ở cuối bài)
    Nhân đây em cũng upload cái MobileShop này lên cho bác và các anh em xem qua rồi cho ý kiến giùm em với!
    Và trong cái MobiShop này có 2 lỗi khá nghiêm trọng em chưa fix được.
    ---Lỗi thứ nhất là lỗi thêm sản phẩm
    ---Lỗi thứ 2 là lỗi thêm người dùng (ko hiển thị tên nhóm người dùng để thêm)
    Các bác xem qua và Fix giúp! admin: loan, pass 1234
    Chân thành cảm ơn các bác!

    Đây là link cái MobiShop = aspx, code bằng c#, CSDL SQL Server 2000:
    http://www.mediafire.com/download.php?tymz2i2ngkz

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Code của file quanlyhanghoa.aspx.cs đây ạ:
    Mã:
    using System;
    using System.IO;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Web.Security;
    using System.Configuration;
    
    namespace MobileEcommerce.Admin
    {
    	/// <summary>
    	/// Summary description for AdminProduct.
    	/// </summary>
    	public partial class AdminProduct : System.Web.UI.Page
    	{
    	
    		public string getDSN() 
    		{
    			return ConfigurationSettings.AppSettings["connstring"];
    		}
    		protected void Page_Load(object sender, System.EventArgs e)
    		{
    			// Put user code to initialize the page here
    			if ((IsPostBack == false) || (Request.Params["do"] != "add"))
    			{
    				dgList_BindData();
    			}	
    			if (IsPostBack == false)
    			{
    				isSearch.Checked = false;
    			}
    
    			//check the params to show proper panels and lable messages
    			if (Request.Params["do"] == "add")		
    			{
    				PopulatePnlAdd();
    				lblTitle.Text = "Thêm sản phẩm";
    			}
    			else if (Request.Params["added"] == "1")
    			{
    				lblmsg.Text = "Sản phẩm đã được thêm vào.";
    			}
    			else if (Request.Params["updated"] == "1")
    			{
    				lblmsg.Text = "Sản phẩm đã được cập nhật.";
    			}
    			else if (Request.Params["deleted"] == "1")
    			{
    				lblmsg.Text = "Sản phẩm đã được xóa thành công";
    			}        
    		}
    		void dgList_BindData()
    		{
    			// bind category info  to datagrid
    			MobileEcommerce.ProductsDB objProducts = new MobileEcommerce.ProductsDB(getDSN());
    			if (isSearch.Checked == true)
    			{
    				dgList.DataSource = objProducts.SearchProductAdmin(txtsearch.Text);						
    			}
    			else
    			{
    				dgList.DataSource = objProducts.GetAllProducts();
    			}
    			dgList.DataBind();	
    		}		
    
    
    		// *****************************************************************************************************
    		//    dgList_PageIndexChanged() method
    		//    DataGrid Paging for Product Info
    		// *****************************************************************************************************
    
    		private void dgList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    		{
    			// bind Product info  to datagrid on each page change event
    			dgList.CurrentPageIndex = e.NewPageIndex;
    			dgList_BindData();
    		}
    
    		// *****************************************************************************************************
    		//    This method handle the DataGrid dgList// s ItemCommand, 
    		//    determine whether the ItemCommand is Edit Command or Delete Command to show the proper panel 
    		//    and populate the product detail data. 
    		//    The method calls methods in the ProductDB class: GetProductDetails() ; GetProductCategories(); 
    		// ********************************************************************************************************
    		private void dgList_ItemCommand(object source, DataGridCommandEventArgs e)
    		{
    			//        //show the proper panel for clicking the Edit & Delete links
    			if (e.CommandName == "edit")
    			{
    //				// show pnlEdit panels
    				pnlAll.Visible = false;
    				pnlEdit.Visible = true;
    				// populate fields with DB data
    				MobileEcommerce.ProductDetails objProdDetails;
    				MobileEcommerce.ProductsDB objProduct = new MobileEcommerce.ProductsDB(getDSN());
    				objProdDetails = objProduct.GetProductDetails((int)(this.dgList.DataKeys[e.Item.ItemIndex]));
    
    				// lblmsg.Text += dgList.DataKeys[e.Item.ItemIndex] + " " + dgList.SelectedIndex;
    				
                    //txtEditCost.Text = Decimal.Truncate(objProdDetails.UnitCost).ToString();
    				txtEditPrice.Text = Decimal.Truncate(objProdDetails.UnitPrice).ToString();
    				txtEditName.Text = objProdDetails.ProductName;
    				ftbEditDesc.Text = objProdDetails.Description;
    				ftbEditSpec.Text = objProdDetails.Specification;
    				txtEditQuantity.Text = objProdDetails.QuantityInStock.ToString();
                    //
                    erdBL.Checked = (objProdDetails.bluetooth == 1);
                    erdBNN.Checked = (objProdDetails.bonhongoai == 1);
                    erdBNT.Checked = (objProdDetails.bonhotrong == 1);
                    erdBT.Checked = (objProdDetails.baothuc == 1);
                    erdDK.Checked = (objProdDetails.dkgiongnoi == 1);
                    erdMA.Checked = (objProdDetails.mayanh == 1);
                    erdNN.Checked = (objProdDetails.nghenhac == 1);
                    //
                    epr_rd1.Text = objProdDetails.gut1.ToString();
                    epr_rd2.Text = objProdDetails.gut2.ToString();
                    epr_rd3.Text = objProdDetails.gut3.ToString();
                    ext_rd1.Text = objProdDetails.xut1.ToString();
    
                    ext_rd2.Text = objProdDetails.xut2.ToString();
                    ext_rd3.Text = objProdDetails.xut3.ToString();
                    eth_rd1.Text = objProdDetails.dut1.ToString();
                    eth_rd2.Text = objProdDetails.dut2.ToString();
    
                    eth_rd3.Text = objProdDetails.dut3.ToString();
                    emm_rd1.Text = objProdDetails.kut1.ToString();
                    emm_rd2.Text = objProdDetails.kut2.ToString();
                    emm_rd3.Text = objProdDetails.kut3.ToString();
    	
    				if (objProdDetails.LargeProductImage != "")
    				{
    					imgEditImage.ImageUrl = @"..\images\ProductImages\Large\" + objProdDetails.LargeProductImage;
    					imgEditImage.Visible = true;					
    					strLargeImage.Text = objProdDetails.LargeProductImage.ToString();
    				}
    				if (objProdDetails.SmallProductImage != "")
    				{
    					imgEditThumbImage.ImageUrl = @"..\images\ProductImages\Thumbs\" + objProdDetails.SmallProductImage;
    					imgEditThumbImage.Visible = true;
    					strSmallImage.Text = objProdDetails.SmallProductImage;
    				}
    				// populate DropDownList ddlEditCategory with Category information
    				SqlDataReader dtrReader = objProduct.GetProductCategories();
    	
    				while (dtrReader.Read())
    				{
    					ListItem myItem = new ListItem();
    					myItem.Text = dtrReader["CategoryName"].ToString();
    					myItem.Value = dtrReader["CategoryID"].ToString();
    					ddlEditCategory.Items.Add(myItem);
    				}
    	
    				// get Selected Item From Category from product list datagrid
    				int counter = 0;
    				foreach (ListItem itemCat in ddlEditCategory.Items)
    				{
    					if (itemCat.Text.Trim() == e.Item.Cells[1].Text)
    					{
    						ddlEditCategory.SelectedIndex = counter;
    					}
    					counter = counter + 1;
    				}
    	
    				// pass the CatID to a form field in the EditCats panel
    				EditProductID.Text = dgList.DataKeys[e.Item.ItemIndex].ToString();
    			}
    			else if (e.CommandName == "delete")
    			{
    				// the Delete ItemCommand is redirected to Confirm delete page
    				Response.Redirect("DeleteProduct.aspx?pid=" + dgList.DataKeys[e.Item.ItemIndex]);
    			}
    		}
    
        // *****************************************************************************************************
        //    PopulatePnlAdd() method
        //   if they have clicked on the // Add Product' link -- populate pnlAdd and make visible
        // *****************************************************************************************************
    
    		private void PopulatePnlAdd()
    		{
    			pnlAll.Visible = false;
    			pnlAdd.Visible = true;
    			lblSearch.Visible = false;
    			txtsearch.Visible = false;
    			btnGo.Visible = false;
    
    			// populate the Add Product -- Category DDL
    			MobileEcommerce.ProductsDB objProduct = new MobileEcommerce.ProductsDB(getDSN());
    			SqlDataReader dtrReader;
    
    			dtrReader = objProduct.GetProductCategories();
    			while (dtrReader.Read())
    			{
    				ListItem myItem = new ListItem();
    				myItem.Text = dtrReader["CategoryName"].ToString();
    				myItem.Value = dtrReader["CategoryID"].ToString();
    				ddlCategory.Items.Add(myItem);
    			}
    		}
    		 // *****************************************************************************************************
    		//   SaveProduct() method
    		//    This method adds a new Product to database by calling AddNewProduct() class in ProductDB
    		//    The image of product is uploaded to Images folder on server after check their type, this require calling 
    		//    ValidFileType() method in CommonUtils class 
    		// *****************************************************************************************************
    		
    		private void btnSaveProduct_Click(object s, System.EventArgs e)
    		{	
    			MobileEcommerce.ProductsDB objNewProduct = new MobileEcommerce.ProductsDB(getDSN());
    			MobileEcommerce.CommonUtils objFileType = new MobileEcommerce.CommonUtils();
    
    			// this is the path where the image uploads are stored -- both large and thumb size images for products
    			string VirtualPath = Server.MapPath(Request.ServerVariables["URL"]);
                string ImageSavePath = VirtualPath.Substring(0, VirtualPath.IndexOf("Admin") - 1) + @"\images\ProductImages\Large\";
                string ThumbSavePath = VirtualPath.Substring(0, VirtualPath.IndexOf("Admin") - 1) + @"\images\ProductImages\Thumbs\";
    	
    			// save the name of the image file they are uploading to DB as well as the rest of the info --  
    			// make sure they are uploading an image
    			string filename = Path.GetFileName(txtImage.PostedFile.FileName).Trim();
    			string thumbfilename = Path.GetFileName(txtThumbImage.PostedFile.FileName).Trim();
    		
    			//  check the proper image file type
    			if ((filename != "") && (objFileType.ValidFileType(filename) == false))
    			{																			 
    				lblmsg.Text = "Bạn chỉ có thể Upload file: .jpg, .jpeg or .gif";
    			}
    			else if ((thumbfilename != "") && (objFileType.ValidFileType(thumbfilename) == false))
    			{
    				lblmsg.Text = "Bạn chỉ có thể Upload file: .jpg, .jpeg or .gif";
    			}
    			else
    			{
    				if (filename == "")
    				{
    					filename = "not-available.jpg";
    				}
    				if (thumbfilename == "")
    				{
    					thumbfilename = "not-available.jpg";
    				}				
    				// save the Product info to the db, if success return the productID				
                    int result =objNewProduct.AddNewProduct(txtName.Text.Trim(), Convert.ToInt32(ddlCategory.SelectedItem.Value), filename.ToString(), thumbfilename.ToString(), Convert.ToDecimal(txtPrice.Text), Convert.ToDecimal(txtPrice.Text), ftbDesc.Text, ftbSpec.Text, Convert.ToInt32(txtQuantity.Text.Trim()),
                        rdBL.Checked,rdBT.Checked,rdDK.Checked,rdMA.Checked,rdNN.Checked,rdBNT.Checked,rdBNN.Checked);
                    
    				if (result > 0)// if the method AddNewProduct is success
    				{
    					// if there was an image added, upload image, otherwise, redirect back to AdminProduct.aspx
    					// upload the image to its folder
                        try
                        {
                            if (filename != "not-available.jpg")
                            {
                                // upload the image to its folder
                                System.Web.HttpPostedFile postedFile = txtImage.PostedFile;
                                string contentType = postedFile.ContentType;
                                int contentLength = postedFile.ContentLength;
                                postedFile.SaveAs(ImageSavePath + filename);
                            }
    
                            //if there was an thumb added, upload it, else, redirect back to AdminProduct.aspx
                            if (thumbfilename != "not-available.jpg")
                            {
                                // upload the thumbnail image to its folder
                                System.Web.HttpPostedFile postedFileThumb = txtThumbImage.PostedFile;
                                string contentTypeThumb = postedFileThumb.ContentType;
                                int contentLengthThumb = postedFileThumb.ContentLength;
                                postedFileThumb.SaveAs(ThumbSavePath + thumbfilename);
                            }
                        }
                        catch { }
    					Response.Redirect("Quanlyhanghoa.aspx?added=1");
    				}
    				else if (result == -1)
    				{
    					lblmsg.ForeColor = Color.Red;
    					lblmsg.Text = "Sản phẩm đã tồn tại hoặc 'Thêm sản phẩm' đã bị lỗi. Xin hãy thử lại.";
    				}
    				else
    				{
    					lblmsg.Text = "Có vấn đề với đầu vào. Xin hãy thử lại.";
    				}
    				// this is the end if statment to make sure they are uploading .gif, .jpg or .jpeg images
    			}
    		}
    
    //		*****************************************************************************************************
    //		UpdateProduct() method
    //		This method handles btnEditProduct.Click event to update an existing Product Information by 
    //		calling UpdateProduct() method in ProductDB class
    //		*****************************************************************************************************
    		private void btnEditProduct_Click(object s, System.EventArgs e)
    		{
    			MobileEcommerce.ProductsDB objEditProduct = new MobileEcommerce.ProductsDB(getDSN());
    			CommonUtils objFileType = new CommonUtils();
    
    			// this is the path where the image uploads are stored -- both large and thumb size images for products
    			string VirtualPath = Server.MapPath(Request.ServerVariables["URL"]);
    			string ImageSavePath = VirtualPath.Substring(0, VirtualPath.IndexOf("Admin")) + @"\images\ProductImages\Large\";
    			string ThumbSavePath = VirtualPath.Substring(0, VirtualPath.IndexOf("Admin")) + @"\images\ProductImages\Thumbs\";
    	
    			// save the name of the image file they are uploading to DB as well as the rest of the info --  
    			// make sure they are uploading an image
    			string filename = Path.GetFileName(txtEditImage.PostedFile.FileName).Trim();
    			string thumbfilename = Path.GetFileName(txtEditThumbImage.PostedFile.FileName).Trim();
    
    			//  check the proper image file type
    			if ((filename != "") && (objFileType.ValidFileType(filename) == false))
    			{																			 
    				lblmsg.Text = "Bạn chỉ có thể Upload file: .jpg, .jpeg or .gif";
    			}
    			else if ((thumbfilename != "") && (objFileType.ValidFileType(thumbfilename) == false))
    			{
    				lblmsg.Text = "Bạn chỉ có thể Upload file: .jpg, .jpeg or .gif";
    			}
    			else
    			{
    				if (filename == "")
    				{
    					filename = strLargeImage.Text;
    				}
    				if (thumbfilename == "")
    				{
    					thumbfilename = strSmallImage.Text;
    				}		
    	
    				// Update the Product info to the db				
    
                    int result = objEditProduct.UpdateProduct(Convert.ToInt32(EditProductID.Text), txtEditName.Text.Trim(), Convert.ToInt32(ddlEditCategory.SelectedItem.Value), filename.ToString(), thumbfilename.ToString(), Convert.ToDecimal(txtPrice.Text), Convert.ToDecimal(txtEditPrice.Text), ftbEditDesc.Text, ftbEditSpec.Text, Convert.ToInt32(txtEditQuantity.Text.Trim()),
                         erdBL.Checked, erdBT.Checked, erdDK.Checked, erdMA.Checked, erdNN.Checked, erdBNT.Checked, erdBNN.Checked, epr_rd1.Text, epr_rd2.Text,
                        epr_rd3.Text, ext_rd1.Text, ext_rd2.Text, ext_rd3.Text, emm_rd1.Text, emm_rd2.Text, emm_rd3.Text, eth_rd1.Text, eth_rd2.Text, eth_rd3.Text);
    				
    				if (result > 0)
    				{
                        try
                        {
                            // if there was an image added, upload image, otherwise, redirect back to AdminProduct.aspx
                            if (Path.GetFileName(txtEditImage.PostedFile.FileName).Trim() != "")
                            {
                                // upload the image to its folder
                                System.Web.HttpPostedFile postedFile = txtEditImage.PostedFile;
                                string contentType = postedFile.ContentType;
                                int contentLength = postedFile.ContentLength;
                                postedFile.SaveAs(ImageSavePath + filename); //  cause err when update the product that has not got image
                            }
                            if (Path.GetFileName(txtEditThumbImage.PostedFile.FileName) != "")
                            {
                                // upload the thumbnail image to its folder
                                System.Web.HttpPostedFile postedFileThumb = txtEditThumbImage.PostedFile;
                                string contentTypeThumb = postedFileThumb.ContentType;
                                int contentLengthThumb = postedFileThumb.ContentLength;
                                postedFileThumb.SaveAs(ThumbSavePath + thumbfilename);
                            }
                        }
                        catch { }
    					Response.Redirect("Quanlyhanghoa.aspx?updated=1");
    				}
    				else 
    				{
    					lblmsg.Text = "Có vấn đề trong việc cập nhật sản phẩm. Xin hãy thử lại.";
    				}
    	
    				// this is the end if statment to make sure they are uploading .gif, .jpg or .jpeg images
    			}
    		}
    							
    		// *****************************************************************************************************
    		//    btnGo_Click() method
    		//    This method handles btnGo.Click event to do search Product Information with search keyword
    		//    The method is set the isSearch checkbox to True and calling dgList_BindData()method to show the search result
    		// *****************************************************************************************************
    							
    		private void btnGo_Click(object sender, System.EventArgs e)
    		{
    			// show the proper panel
    			pnlAll.Visible = true;
    			pnlAdd.Visible = false;
    			pnlEdit.Visible = false;
    	
    			// set the dgList to bind search result
    			isSearch.Checked = true;
    	
    			dgList.CurrentPageIndex = 0;
    			dgList.SelectedIndex = -1;
    	
    			// bind category info  to datagrid
    			dgList_BindData();
    			lnkSrchBack.Visible = true;
    	
    			//  Hide the list and display a message if no orders have ever been made
    			if (dgList.Items.Count == 0 )
    			{
    	
    				lblmsg.Text = "Không có sản phẩm nào được tìm thấy.";
    				dgList.Visible = false;
    				txtsearch.Visible = false;
    				btnGo.Visible = false;
    				lblSearch.Visible = false;
    			}
    		}
    					
    		#region Web Form Designer generated code
    		override protected void OnInit(EventArgs e)
    		{
    			//
    			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
    			//
    			InitializeComponent();
    			base.OnInit(e);
    		}
    		
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		private void InitializeComponent()
    		{    
    			this.dgList.PageIndexChanged += new DataGridPageChangedEventHandler(dgList_PageIndexChanged);
    			this.dgList.ItemCommand +=new DataGridCommandEventHandler(dgList_ItemCommand);
    			this.btnSaveProduct.Click += new EventHandler(btnSaveProduct_Click);			
    			this.btnGo.Click +=new EventHandler(btnGo_Click);
    			this.btnEditProduct.Click += new EventHandler(btnEditProduct_Click);
    			// Chung thuc
    			HttpCookie oCookie;
    			oCookie = this.Request.Cookies["Ecommerce_AID"];
    			if(oCookie==null)
    			{
    				Response.Redirect("login.aspx?id=false");
    			}
    			MobileEcommerce.AdminDB objRequest = new MobileEcommerce.AdminDB(getDSN());
    			if (objRequest.RequestLogin(Int32.Parse(Request.Cookies["Ecommerce_AID"].Value), "Administrator Management") == false)
    			{
    				Response.Redirect("ErrorPage.aspx");
    			}  
    		}
    		#endregion
    		   
    	}
    }
    Còn đây là lỗi khi admin thêm sản phẩm:

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mã:
    private void btnSaveProduct_Click(object s, System.EventArgs e) ................ //tại đây bạn nên kiểm tra coi thử thằng này có lớn hơn -1 hay không            string VirtualPath = Server.MapPath(Request.ServerVariables["URL"]);            int i = VirtualPath.IndexOf("Admin");            if (i > 0)            {                string si = VirtualPath.Substring(0, i);                string ImageSavePath = VirtualPath.Substring(0,-1 - 1) + @"\images\ProductImages\Large\";                        string ThumbSavePath = VirtualPath.Substring(0, i - 1) + @"\images\ProductImages\Thumbs\";             }
    lỗi trên là do trong chuổi VirtualPath nó không có chuổi Admin. nên khi chạy nó báo lỗi như trường hợp bạn thấy.[IMG]images/smilies/smile.png[/IMG]



    chúc bạn thành công.

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Khi Admin thêm sản phẩm thì báo lỗi! giúp em với!

    Em đang làm website bán điện thoại di động bằng asp.net và C#. Nhưng gặp lỗi xảy ra ở trang quản lý loại hàng. Cụ thể khi Admin thêm sản phẩm mới thì báo lỗi "Length is less than zero" và gạch đỏ vào dòng code C# có chứa đường dẫn ảnh "\images\ProductImages\Large".
    Em gửi trang quanlyloaihang lên để các bác xem giúp em với!
    Cảm ơn các bác nhìu!
    (code dài nên em nén .rar ạ)

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn đưa thêm cấu trúc của những table lên đây luôn nhé. mình nghĩ do cấu trúc table của bạn.
    bạn thử bằng câu lệnh insert với dữ liệu y như bạn lấy được bên chương trình mang qua bên database run với câu insert đó thử nó bị lỗi không.

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Các bác ơi, bớt chút thời gian xem giúp em với!
    Em đang cần fix 2 lỗi đó gấp mà ko biết hỏi ai!
    Em sẽ cảm ơn và hậu tạ bằng 1 chầu cafe nếu các bác ở HN!

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Các bác à, em xem lại thì lỗi chính xác xảy ra như sau:
    Khi em triệu gọi trang admin và đăng nhập, rồi chọn mục thêm sản phẩm thì nó xảy ra những lỗi trên!
    Nhưng khi em F5 cái trang quanlyloaihang.aspx.cs thì nó yêu cầu đăng nhập bằng admin và lúc này em chọn thêm sản phẩm thì lại được!
    Vài dòng báo cáo để các bác xem giúp!

  8. #8
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    ủa sao mình chạy nó vẫn bình thường mà bạn.
    không có lỗi gì xảy ra cả.

    nếu vậy bạn thử đặt break point chổ nào mà bạn nghi là sai rồi debug thử xem sao....

    chúc bạn thành công.

  9. #9
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Cảm ơn anh zkday2686!
    Em check lại rồi:
    Nếu em vào trang admin theo kiểu .../mobileShop/Admin thì nó lỗi như em đã trình bày ở trên
    Nếu em vào trang admin theo kiểu .../MobileShop/Admin/default.aspx thì ko còn lỗi thêm sản phẩm
    Như vậy lỗi thêm sản phẩm có thể coi là được khắc phục
    Nhưng vẫn còn lỗi ở phần quản lí người dùng là ko thêm được người dùng
    Anh xem giúp em với nhé!

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •