我正在为我的最后一年开发一个ASP.NET项目,目前正在开发#34; Admin"模块,我被困在下面给出的这部分代码中。我正在获取"服务"的对象。通过方法" SelectByID(int id)"通过Business Logic。此方法返回" Service"的对象。现在,当我将此对象存储到Session中并查看对象的属性时,ID将存储为' 0'。我需要在同一页面的另一种方法中使用此ID,我将通过FileUploadControl更改显示图片。我附上了我的方法代码,方法的C#代码,以及对象属性的屏幕截图。
<pre><code>public Service SelectByID(int ServiceID)
{
string query = "select * from tblService where ServiceID=@ServiceID ";
List<SqlParameter> lstParams = new List<SqlParameter>();
lstParams.Add(new SqlParameter("@ServiceID", ServiceID));
DataTable dt = DBUtility.SelectData(query, lstParams);
Service serviceObj = new Service();
if (dt.Rows.Count > 0)
{
serviceObj.Name = dt.Rows[0]["Name"].ToString();
serviceObj.Description = dt.Rows[0]["Description"].ToString();
serviceObj.Photo = dt.Rows[0]["Photo"].ToString();
}
return serviceObj;
}
//Page Load Code
protected void Page_Load(object sender, EventArgs e)
{
if (Session["admin"] != null)
{
if (!IsPostBack)
{
ServiceLogic serviceLogic = new ServiceLogic();
int id = Convert.ToInt32(Request.QueryString["Id"]); // Here ID is recieved properly from QueryString
Service service = serviceLogic.SelectByID(id); //Here too ID is recieved(obviously)
Session["service"] = service; //Here the ServiceID is storing '0'
lblService.Text = service.Name;
txtServiceName.Text = service.Name;
txtDescription.Text = service.Description;
if (service.Photo == "")
{
imgService.ImageUrl = "~/Images/defaultVendor.png";
}
else
{
imgService.ImageUrl = service.Photo;
}
}
}
else
{
Response.Redirect("AdminLogin.aspx");
}
}
//Update profile pic code. Assume the above code where file extensions and file size are checked!
{
string filePath = Server.MapPath("~/Images/ServicesPhoto/" + fuServiceImage.FileName);
fuServiceImage.SaveAs(filePath);
lblServiceUpdate.Text = "Display Picture Changed Successfully!";
lblServiceUpdate.ForeColor = System.Drawing.Color.Green;
string imagePath = "~/Images/ProfilePics/" + fuServiceImage.FileName;
imgService.ImageUrl = imagePath;
Service service = (Service)Session["service"];
serviceLogic.updateServiceImage(imagePath, service.ServiceID);
service.Photo = imagePath;
lblService.Text = service.Name;
txtServiceName.Text = service.Name;
txtDescription.Text = service.Description;
imgService.ImageUrl = service.Photo;
//Response.Redirect("ServiceDetails.aspx");
}
答案 0 :(得分:2)
您必须从服务方法
获取IDpublic Service SelectByID(int ServiceID)
{
string query = "select * from tblService where ServiceID=@ServiceID ";
List<SqlParameter> lstParams = new List<SqlParameter>();
lstParams.Add(new SqlParameter("@ServiceID", ServiceID));
DataTable dt = DBUtility.SelectData(query, lstParams);
Service serviceObj = new Service();
if (dt.Rows.Count > 0)
{
serviceObj.ID = dt.Rows[0]["ServiceID"].ToString();
serviceObj.Name = dt.Rows[0]["Name"].ToString();
serviceObj.Description = dt.Rows[0]["Description"].ToString();
serviceObj.Photo = dt.Rows[0]["Photo"].ToString();
}
return serviceObj;
}
在服务数据获取方法中添加ID对象
serviceObj.ID = dt.Rows[0]["ServiceID"].ToString();
答案 1 :(得分:0)
您似乎没有获取并将Id存储到Service对象中。
尝试以下方法:
public Service SelectByID(int ServiceID)
{
string query = "select * from tblService where ServiceID=@ServiceID ";
List<SqlParameter> lstParams = new List<SqlParameter>();
lstParams.Add(new SqlParameter("@ServiceID", ServiceID));
DataTable dt = DBUtility.SelectData(query, lstParams);
Service serviceObj = new Service();
if (dt.Rows.Count > 0)
{
serviceObj.ID = dt.Rows[0]["ID"].ToString();
// Or you can use
// serviceObj.ID = ServiceID;
serviceObj.Name = dt.Rows[0]["Name"].ToString();
serviceObj.Description = dt.Rows[0]["Description"].ToString();
serviceObj.Photo = dt.Rows[0]["Photo"].ToString();
}
return serviceObj;
}
请不要忘记标记它是否可以解决您的问题。