[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 667
  • Last Modified:

How and where do I declare the variable to fix this issue? non-coder

Here is the error message I get when saving to my ADMIN
What does it mean and how can I fix it?  I have attached code and file that are involved.

Compiler Error Message: CS0103: The name 'artistID' does not exist in the current context

Source Error:

Line 95: try Line 96: { Line 97: UploadImage(image1, artistID); Line 98: } Line 99: catch { }

d:\Domains\amariahart.com\wwwroot\admin\ManageArtists.aspx.cs(97,24): error CS0103: The name 'artistID' does not exist in the current context
d:\Domains\amariahart.com\wwwroot\admin\ManageArtists.aspx.cs(106,38): error CS0103: The name 'artistID' does not exist in the current context
d:\Domains\amariahart.com\wwwroot\admin\ManageArtists.aspx.cs(119,7): warning CS0219: The variable 'photoID' is assigned but its value is never used


Here is ManageArtists.aspx I will attach ManageArtists.aspx.cs 
 
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" ValidateRequest="false" CodeFile="ManageArtists.aspx.cs" Inherits="admin_ManageArtists" Title="Manage Artists" Debug="true"%>
<%@ Register TagPrefix="FTB" Namespace="FreeTextBoxControls" Assembly="FreeTextBox" %>
 
<asp:Content ID="Content2" ContentPlaceHolderID="cpMain" Runat="Server">
<%--<asp:UpdatePanel ID="upArtist" runat="server" UpdateMode="conditional">
<ContentTemplate>--%>
	<asp:HiddenField ID="hdArtistID" runat="server" />
	<table class="tblForm">
	<tr>
		<td class="formField">Select an Artist:</td>
		<td class="dataField"><asp:DropDownList ID="selArtistID" runat="server" CssClass="combobox" DataSourceID="dsArtists" DataTextField="ArtistName" DataValueField="ArtistID" onchange="javascript:window.location.href='ManageArtists.aspx?id=' + this.value;"></asp:DropDownList></td>
	</tr>
	<tr>
		<td class="formField">Artist Name:</td>
		<td class="dataField"><asp:TextBox ID="txtArtistName" runat="server" CssClass="combobox" Width="150px" MaxLength="50"></asp:TextBox></td>
	</tr>
	<tr>
		<td class="formField">Active:</td>
		<td class="dataField"><asp:CheckBox ID="chkActive" runat="server" /></td>
	</tr>
	<tr>
		<td class="formField" valign="top">Image:</td>
		<td class="dataField" colspan="3">
			<asp:FileUpload ID="image1" runat="server" Width="350px" />
			<div id="divCurrentImages" runat="server" style="height:auto;width:500px;padding:10px 10px 10px 10px;background-color:white;border:0px solid #555;"></div>
		</td>
	</tr>
	</table>
	<span class="text4b"><b>Description:</b></span><br />
	<FTB:FreeTextBox id="txtDescription" Width="500px" Height="300px" runat="Server" BreakMode="LineBreak" DesignModeCss="~/style.css"/>
	<div style="margin-top:15px;"><asp:button cssclass="buttonSkinL L" id="btnLogin" Text="Save" onclick="btnSave_Click" runat="server"/></div>
	
	<asp:SqlDataSource ID="dsArtists" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnectionString %>"
		SelectCommand="SELECT 0 AS ArtistID, 'create new artist' AS ArtistName UNION SELECT ArtistID, ArtistName FROM Artists ORDER BY ArtistName">
	</asp:SqlDataSource>
 
	<script language="javascript">
	function LoadImage(src)
	{
		window.open(src);
	}
	function DeleteImage(artistID)
	{
		getHttpRequest("getImages.aspx?guid=<%= Guid.NewGuid().ToString() %>&action=delArtistImage&artistID=" + artistID,"DisplayImages",false,null);
	}
	function DisplayImages()
	{
		retVal = oHttpRequest.responseText.trim();
		document.getElementById("<%= divCurrentImages.ClientID %>").innerHTML = retVal;
	}
	function GetImages()
	{
		if(document.getElementById("<%= hdArtistID.ClientID %>"))
		{
			var artistID = document.getElementById("<%= hdArtistID.ClientID %>").value;
			getHttpRequest("getImages.aspx?guid=<%= Guid.NewGuid().ToString() %>&action=getArtistImage&artistID=" + artistID,"DisplayImages",false,null);
		}
	}
	GetImages();
	</script>
</asp:Content>

Open in new window

ManageArtist.txt
0
Sandra_joy
Asked:
Sandra_joy
  • 7
  • 5
  • 4
  • +1
1 Solution
 
CEHJCommented:
You're in the wrong TA: ASP != Java
0
 
OBonioCommented:
You want to move var artistId out of GetImages and make it a global.

        <script language="javascript">
        var artistID;
        function LoadImage(src)
        {
                window.open(src);
        }
        function DeleteImage(artistID)
        {
                getHttpRequest("getImages.aspx?guid=<%= Guid.NewGuid().ToString() %>&action=delArtistImage&artistID=" + artistID,"DisplayImages",false,null);
        }
        function DisplayImages()
        {
                retVal = oHttpRequest.responseText.trim();
                document.getElementById("<%= divCurrentImages.ClientID %>").innerHTML = retVal;
        }
        function GetImages()
        {
                if(document.getElementById("<%= hdArtistID.ClientID %>"))
                {
                        artistID = document.getElementById("<%= hdArtistID.ClientID %>").value;
                        getHttpRequest("getImages.aspx?guid=<%= Guid.NewGuid().ToString() %>&action=getArtistImage&artistID=" + artistID,"DisplayImages",false,null);
                }
        }
        GetImages();
        </script>

Open in new window

0
 
OBonioCommented:
Sorry, ignore that.  Just uncomment the declaration of artistId in the .cs file.  Line 89.

int artistID = Convert.ToInt32((string)cmd.ExecuteScalar());

Open in new window

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
Sandra_joyAuthor Commented:

What do you mean uncomment the declaration of artistId ??
Do you mean change this:
//int artistID = Convert.ToInt32((string)cmd.ExecuteScalar());
into this:
int artistID = Convert.ToInt32((string)cmd.ExcuteScalar());  
Remember you are talking to a non-coder.  I was told in another related question that I must "declare the variable".
what does it mean : The name 'artistID' does not exist in the current context

and what does this mean:  The variable 'photoID' is assigned but its value is never used
Thank you.
Sandra


0
 
OBonioCommented:
Yes, basically delete the two forward slashes.

In programming, the places where we store data need to be defined first before we can use them so we know what it is (a number, some text, a decimal ... there are LOTS of these).  The two forward slashes are removing the line where artistID is defined so removing them will make the program aware of it and what type it is (in this case a whole number or integer).

The photoID warning is just that.  It could be ignored.  All it's saying is that you've defined photoID but it never gets used.
0
 
Sandra_joyAuthor Commented:
Ok, I did that and now I get the old error message that the expert from my first question helped me with.  I am just going in circles now.  Below is the error message I get now (again).    The problem may be deeper.  Is it possible that the person who made my website left out something in the admin?  I was the only artist at the time but asked for a way to add other artist with my admin.  Maybe there is no page/file yet to post the photo ID and the Artist ID for the new artist I am trying to add.  
  Server Error in '/' Application. Input string was not in a correct format. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

Line 85: cmd.Connection = cn; Line 86: cn.Open(); Line 87: string strtest = cmd.ExecuteScalar().ToString(); Response.Write("value:" + strtest); Line 88: Line 89: int artistID = Convert.ToInt32((string)cmd.ExecuteScalar());
Source File: d:\Domains\amariahart.com\wwwroot\admin\ManageArtists.aspx.cs    Line: 87

 
0
 
Sandra_joyAuthor Commented:
Ok Now I added the //  back and put in the javascript below (you gave to make it global),  just to see if it helped.  
I got this error message again:
Compiler Error Message: CS0103: The name 'artistID' does not exist in the current context

Source Error:

Line 95: try Line 96: { Line 97: UploadImage(image1, artistID); Line 98: } Line 99: catch { }
Source File: d:\Domains\amariahart.com\wwwroot\admin\ManageArtists.aspx.cs    Line: 97

what is your expert opinion of the problem and what steps should I take or what question should I ask?
0
 
OBonioCommented:
Why did you add the // back?  Merely commenting out that line is not a solution to the problem.  The issue you are experiencing is to do with the SQL command not returning a valid integer.

string sqlResult = (string)cmd.ExecuteScalar();
int artistID = int.MinValue;
if(int.TryParse(sqlResult, out artistId))
{
  .. other code here
}

Open in new window

0
 
Sandra_joyAuthor Commented:
Could you please paste it into the file below as I am not finding or following what you mean?
public partial class admin_ManageArtists : System.Web.UI.Page
{
	string sqlstr = "";
	SqlCommand cmd = new SqlCommand();
	SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString);
	SqlDataReader dr;
 
    protected void Page_Load(object sender, EventArgs e)
    {
		if (Request.QueryString["id"] != "" && Request.QueryString["id"] != null && !IsPostBack)
		{
			GetArtist(Convert.ToInt32(Request.QueryString["id"]));
		}
    }
 
	protected void GetArtist(int artistID)
	{
		if (artistID == 0)
		{
			hdArtistID.Value = "0";
			txtArtistName.Text = "";
			txtDescription.Text = "";
			chkActive.Checked = true;
		}
		else
		{
			sqlstr = "SELECT ArtistID, ArtistName, Description, ImageURL, Active FROM Artists WHERE ArtistID = @ArtistID";
			cmd.CommandText = sqlstr;
			cmd.Connection = cn;
			cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = int.Parse(hdArtistID.Value);
			cn.Open();
 
			dr = cmd.ExecuteReader();
 
			dr.Read();
			if (dr.HasRows)
			{
				hdArtistID.Value = dr["ArtistID"].ToString();
				txtArtistName.Text = dr["ArtistName"].ToString();
				txtDescription.Text = dr["Description"].ToString();
				chkActive.Checked = Convert.ToBoolean(dr["Active"]);
			}
			cmd.Parameters.Clear();
			cn.Close();
			dr.Close();
		}
	}
 
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (hdArtistID.Value == "0")
		{
			sqlstr = "INSERT INTO Artists (ArtistName, Description, Active) VALUES (@ArtistName, @Description, @Active); "
				+ "SELECT IDENT_CURRENT('Artists') AS ArtistID;";
		}
		else
		{
			sqlstr = "UPDATE Artists SET ArtistName = @ArtistName, Description = @Description, "
				+ "Active = @Active WHERE ArtistID = @ArtistID; "
				+ "SELECT @ArtistID AS ArtistID;";
			cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = hdArtistID.Value;
		}
 
		cmd.Parameters.Add("@ArtistName", SqlDbType.NVarChar).Value = txtArtistName.Text;
		cmd.Parameters.Add("@Description", SqlDbType.NText).Value = txtDescription.Text;
		cmd.Parameters.Add("@ImageURL", SqlDbType.NVarChar).Value = "";
		cmd.Parameters.Add("@Active", SqlDbType.Bit).Value = chkActive.Checked;
		cmd.CommandText = sqlstr;
		cmd.Connection = cn;
		cn.Open();
       string strtest = cmd.ExecuteScalar().ToString(); Response.Write("value:" + strtest);
 
		//int artistID = Convert.ToInt32((string)cmd.ExecuteScalar());
 
		cn.Close();
		cmd.Parameters.Clear();
 
		//upload image
		try
		{
			UploadImage(image1, artistID);
		}
		catch { }
 
 
		//select theme
		selArtistID.DataBind();
		for (int i = 0; i < selArtistID.Items.Count; i++)
		{
			if (selArtistID.Items[i].Value == artistID.ToString())
			{
				selArtistID.Items[i].Selected = true;
				break;
			}
		}
	}
 
	protected void UploadImage(FileUpload fPhoto, int artistID)
	{
		bool fileError = false;
		string fnOrig = "",
			fnNew = "";
		int photoID = 0;
		//upload photo
		if (fPhoto.PostedFile != null && fPhoto.PostedFile.ContentLength > 0)
		{
			try
			{
				//save file
				fnOrig = Guid.NewGuid().ToString() + "_o" + System.IO.Path.GetExtension(fPhoto.PostedFile.FileName);
				fnNew = Guid.NewGuid().ToString() + "_c" + System.IO.Path.GetExtension(fPhoto.PostedFile.FileName);
				fPhoto.PostedFile.SaveAs(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig);
 
				//resize & compress image
				System.Drawing.Image img = System.Drawing.Image.FromFile(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig);
				SaveJPGWithCompressionSetting(img, ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnNew, 60L);
 
			}
			catch
			{
				fileError = true;
			}
 
			//add photo to db
			if (!fileError)
			{
				//add new photo
				sqlstr = "UPDATE Artists SET ImageURL = @ImageURL WHERE ArtistID = @ArtistID";
 
				cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = artistID;
				cmd.Parameters.Add("@ImageURL", SqlDbType.NVarChar).Value = fnNew;
 
				cmd.CommandText = sqlstr;
				cmd.Connection = cn;
				cn.Open();
 
				cmd.ExecuteScalar();
 
				cn.Close();
				cmd.Parameters.Clear();
 
				//delete original file
				try
				{
					if (File.Exists(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig))
					{
						File.Delete(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig);
					}
				}
				catch { }
 
			}
		}
 
	}
 
	private static ImageCodecInfo GetEncoderInfo(String mimeType)
	{
		int j;
		ImageCodecInfo[] encoders;
		encoders = ImageCodecInfo.GetImageEncoders();
		for (j = 0; j < encoders.Length; ++j)
		{
			if (encoders[j].MimeType == mimeType)
				return encoders[j];
		}
		return null;
	}
	private void SaveJPGWithCompressionSetting(System.Drawing.Image image, string szFileName, long lCompression)
	{
		EncoderParameters eps = new EncoderParameters(1);
		eps.Param[0] = new EncoderParameter(Encoder.Quality, lCompression);
		ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
		image.Save(szFileName, ici, eps);
	}
}

Open in new window

0
 
Cedric Obinna A.Full Stack DeveloperCommented:
Try this:

Change the code in line 73: //int artistID = Convert.ToInt32((string)cmd.ExecuteScalar());

To These TWO lines:

int artistID
artistID = Convert.ToInt32((string)cmd.ExecuteScalar());

In Line 71, change
Response.Write("value:" + strtest);
TO
Response.Write "value: " & strtest;

Let's see how that goes
0
 
Cedric Obinna A.Full Stack DeveloperCommented:
Or use the following which is your code above with the suggested modification.
public partial class admin_ManageArtists : System.Web.UI.Page
{
	string sqlstr = "";
	SqlCommand cmd = new SqlCommand();
	SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString);
	SqlDataReader dr;
 
    protected void Page_Load(object sender, EventArgs e)
    {
		if (Request.QueryString["id"] != "" && Request.QueryString["id"] != null && !IsPostBack)
		{
			GetArtist(Convert.ToInt32(Request.QueryString["id"]));
		}
    }
 
	protected void GetArtist(int artistID)
	{
		if (artistID == 0)
		{
			hdArtistID.Value = "0";
			txtArtistName.Text = "";
			txtDescription.Text = "";
			chkActive.Checked = true;
		}
		else
		{
			sqlstr = "SELECT ArtistID, ArtistName, Description, ImageURL, Active FROM Artists WHERE ArtistID = @ArtistID";
			cmd.CommandText = sqlstr;
			cmd.Connection = cn;
			cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = int.Parse(hdArtistID.Value);
			cn.Open();
 
			dr = cmd.ExecuteReader();
 
			dr.Read();
			if (dr.HasRows)
			{
				hdArtistID.Value = dr["ArtistID"].ToString();
				txtArtistName.Text = dr["ArtistName"].ToString();
				txtDescription.Text = dr["Description"].ToString();
				chkActive.Checked = Convert.ToBoolean(dr["Active"]);
			}
			cmd.Parameters.Clear();
			cn.Close();
			dr.Close();
		}
	}
 
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (hdArtistID.Value == "0")
		{
			sqlstr = "INSERT INTO Artists (ArtistName, Description, Active) VALUES (@ArtistName, @Description, @Active); "
				+ "SELECT IDENT_CURRENT('Artists') AS ArtistID;";
		}
		else
		{
			sqlstr = "UPDATE Artists SET ArtistName = @ArtistName, Description = @Description, "
				+ "Active = @Active WHERE ArtistID = @ArtistID; "
				+ "SELECT @ArtistID AS ArtistID;";
			cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = hdArtistID.Value;
		}
 
		cmd.Parameters.Add("@ArtistName", SqlDbType.NVarChar).Value = txtArtistName.Text;
		cmd.Parameters.Add("@Description", SqlDbType.NText).Value = txtDescription.Text;
		cmd.Parameters.Add("@ImageURL", SqlDbType.NVarChar).Value = "";
		cmd.Parameters.Add("@Active", SqlDbType.Bit).Value = chkActive.Checked;
		cmd.CommandText = sqlstr;
		cmd.Connection = cn;
		cn.Open();
       string strtest = cmd.ExecuteScalar().ToString(); Response.Write "value: " & strtest;
 
		//int artistID = Convert.ToInt32((string)cmd.ExecuteScalar());
 
int artistID
artistID = Convert.ToInt32((string)cmd.ExecuteScalar());
 
 
		cn.Close();
		cmd.Parameters.Clear();
 
		//upload image
		try
		{
			UploadImage(image1, artistID);
		}
		catch { }
 
 
		//select theme
		selArtistID.DataBind();
		for (int i = 0; i < selArtistID.Items.Count; i++)
		{
			if (selArtistID.Items[i].Value == artistID.ToString())
			{
				selArtistID.Items[i].Selected = true;
				break;
			}
		}
	}
 
	protected void UploadImage(FileUpload fPhoto, int artistID)
	{
		bool fileError = false;
		string fnOrig = "",
			fnNew = "";
		int photoID = 0;
		//upload photo
		if (fPhoto.PostedFile != null && fPhoto.PostedFile.ContentLength > 0)
		{
			try
			{
				//save file
				fnOrig = Guid.NewGuid().ToString() + "_o" + System.IO.Path.GetExtension(fPhoto.PostedFile.FileName);
				fnNew = Guid.NewGuid().ToString() + "_c" + System.IO.Path.GetExtension(fPhoto.PostedFile.FileName);
				fPhoto.PostedFile.SaveAs(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig);
 
				//resize & compress image
				System.Drawing.Image img = System.Drawing.Image.FromFile(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig);
				SaveJPGWithCompressionSetting(img, ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnNew, 60L);
 
			}
			catch
			{
				fileError = true;
			}
 
			//add photo to db
			if (!fileError)
			{
				//add new photo
				sqlstr = "UPDATE Artists SET ImageURL = @ImageURL WHERE ArtistID = @ArtistID";
 
				cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = artistID;
				cmd.Parameters.Add("@ImageURL", SqlDbType.NVarChar).Value = fnNew;
 
				cmd.CommandText = sqlstr;
				cmd.Connection = cn;
				cn.Open();
 
				cmd.ExecuteScalar();
 
				cn.Close();
				cmd.Parameters.Clear();
 
				//delete original file
				try
				{
					if (File.Exists(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig))
					{
						File.Delete(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig);
					}
				}
				catch { }
 
			}
		}
 
	}
 
	private static ImageCodecInfo GetEncoderInfo(String mimeType)
	{
		int j;
		ImageCodecInfo[] encoders;
		encoders = ImageCodecInfo.GetImageEncoders();
		for (j = 0; j < encoders.Length; ++j)
		{
			if (encoders[j].MimeType == mimeType)
				return encoders[j];
		}
		return null;
	}
	private void SaveJPGWithCompressionSetting(System.Drawing.Image image, string szFileName, long lCompression)
	{
		EncoderParameters eps = new EncoderParameters(1);
		eps.Param[0] = new EncoderParameter(Encoder.Quality, lCompression);
		ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
		image.Save(szFileName, ici, eps);
	}
}

Open in new window

0
 
Cedric Obinna A.Full Stack DeveloperCommented:
Sorry... please add a ; at the end of the following:

int artistID

So it becomes

int artistID;
0
 
Sandra_joyAuthor Commented:
Ok this is what I get:
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1002: ; expected

Source Error:

Line 85: cmd.Connection = cn; Line 86: cn.Open(); Line 87: string strtest = cmd.ExecuteScalar().ToString(); Response.Write "value: " & strtest; Line 88: Line 89: //int artistID = Convert.ToInt32((string)cmd.ExecuteScalar());
Source File: d:\Domains\amariahart.com\wwwroot\admin\ManageArtists.aspx.cs    Line: 87


0
 
Cedric Obinna A.Full Stack DeveloperCommented:
Line 87 has this:
string strtest = cmd.ExecuteScalar().ToString();

Change that to:
string strtest;
strtest = cmd.ExecuteScalar().ToString();

Try to ALWAYS declare your variables in one line AND THEN assign them a value in another line. That makes it easier to debug.
0
 
Sandra_joyAuthor Commented:
I can't find the code you are taling about in either of these files:
ManageArtists.aspx  or ManageArtists.aspx.cs  I have attached ManageArtists.aspx below and ManageArtists.aspx.cs  in the code snippet area.

public partial class admin_ManageArtists : System.Web.UI.Page
{
 string sqlstr = "";
 SqlCommand cmd = new SqlCommand();
 SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString);
 SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {
  if (Request.QueryString["id"] != "" && Request.QueryString["id"] != null && !IsPostBack)
  {
   GetArtist(Convert.ToInt32(Request.QueryString["id"]));
  }
    }
 protected void GetArtist(int artistID)
 {
  if (artistID == 0)
  {
   hdArtistID.Value = "0";
   txtArtistName.Text = "";
   txtDescription.Text = "";
   chkActive.Checked = true;
  }
  else
  {
   sqlstr = "SELECT ArtistID, ArtistName, Description, ImageURL, Active FROM Artists WHERE ArtistID = @ArtistID";
   cmd.CommandText = sqlstr;
   cmd.Connection = cn;
   cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = artistID;
   cn.Open();
   dr = cmd.ExecuteReader();
   dr.Read();
   if (dr.HasRows)
   {
    hdArtistID.Value = dr["ArtistID"].ToString();
    txtArtistName.Text = dr["ArtistName"].ToString();
    txtDescription.Text = dr["Description"].ToString();
    chkActive.Checked = Convert.ToBoolean(dr["Active"]);
   }
   cmd.Parameters.Clear();
   cn.Close();
   dr.Close();
  }
 }
 protected void btnSave_Click(object sender, EventArgs e)
 {
  if (hdArtistID.Value == "0")
  {
   sqlstr = "INSERT INTO Artists (ArtistName, Description, Active) VALUES (@ArtistName, @Description, @Active); "
    + "SELECT IDENT_CURRENT('Artists') AS ArtistID;";
  }
  else
  {
   sqlstr = "UPDATE Artists SET ArtistName = @ArtistName, Description = @Description, "
    + "Active = @Active WHERE ArtistID = @ArtistID; "
    + "SELECT @ArtistID AS ArtistID;";
   cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = hdArtistID.Value;
  }
  cmd.Parameters.Add("@ArtistName", SqlDbType.NVarChar).Value = txtArtistName.Text;
  cmd.Parameters.Add("@Description", SqlDbType.NText).Value = txtDescription.Text;
  cmd.Parameters.Add("@ImageURL", SqlDbType.NVarChar).Value = "";
  cmd.Parameters.Add("@Active", SqlDbType.Bit).Value = chkActive.Checked;
  cmd.CommandText = sqlstr;
  cmd.Connection = cn;
  cn.Open();
  int artistID = Convert.ToInt32(cmd.ExecuteScalar());
  cn.Close();
  cmd.Parameters.Clear();
  //upload image
  try
  {
   UploadImage(image1, artistID);
  }
  catch { }

  //select theme
  selArtistID.DataBind();
  for (int i = 0; i < selArtistID.Items.Count; i++)
  {
   if (selArtistID.Items[i].Value == artistID.ToString())
   {
    selArtistID.Items[i].Selected = true;
    break;
   }
  }
 }
 protected void UploadImage(FileUpload fPhoto, int artistID)
 {
  bool fileError = false;
  string fnOrig = "",
   fnNew = "";
  int photoID = 0;
  //upload photo
  if (fPhoto.PostedFile != null && fPhoto.PostedFile.ContentLength > 0)
  {
   try
   {
    //save file
    fnOrig = Guid.NewGuid().ToString() + "_o" + System.IO.Path.GetExtension(fPhoto.PostedFile.FileName);
    fnNew = Guid.NewGuid().ToString() + "_c" + System.IO.Path.GetExtension(fPhoto.PostedFile.FileName);
    fPhoto.PostedFile.SaveAs(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig);
    //resize & compress image
    System.Drawing.Image img = System.Drawing.Image.FromFile(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig);
    SaveJPGWithCompressionSetting(img, ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnNew, 60L);
   }
   catch
   {
    fileError = true;
   }
   //add photo to db
   if (!fileError)
   {
    //add new photo
    sqlstr = "UPDATE Artists SET ImageURL = @ImageURL WHERE ArtistID = @ArtistID";
    cmd.Parameters.Add("@ArtistID", SqlDbType.Int).Value = artistID;
    cmd.Parameters.Add("@ImageURL", SqlDbType.NVarChar).Value = fnNew;
    cmd.CommandText = sqlstr;
    cmd.Connection = cn;
    cn.Open();
    cmd.ExecuteScalar();
    cn.Close();
    cmd.Parameters.Clear();
    //delete original file
    try
    {
     if (File.Exists(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig))
     {
      File.Delete(ConfigurationManager.AppSettings["rootPath"] + "\\artists\\" + fnOrig);
     }
    }
    catch { }
   }
  }
 }
 private static ImageCodecInfo GetEncoderInfo(String mimeType)
 {
  int j;
  ImageCodecInfo[] encoders;
  encoders = ImageCodecInfo.GetImageEncoders();
  for (j = 0; j < encoders.Length; ++j)
  {
   if (encoders[j].MimeType == mimeType)
    return encoders[j];
  }
  return null;
 }
 private void SaveJPGWithCompressionSetting(System.Drawing.Image image, string szFileName, long lCompression)
 {
  EncoderParameters eps = new EncoderParameters(1);
  eps.Param[0] = new EncoderParameter(Encoder.Quality, lCompression);
  ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
  image.Save(szFileName, ici, eps);
 }
}
 

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" ValidateRequest="false" CodeFile="ManageArtists.aspx.cs" Inherits="admin_ManageArtists" Title="Manage Artists" %>
<%@ Register TagPrefix="FTB" Namespace="FreeTextBoxControls" Assembly="FreeTextBox" %>
 
<asp:Content ID="Content2" ContentPlaceHolderID="cpMain" Runat="Server">
<%--<asp:UpdatePanel ID="upArtist" runat="server" UpdateMode="conditional">
<ContentTemplate>--%>
	<asp:HiddenField ID="hdArtistID" runat="server" />
	<table class="tblForm">
	<tr>
		<td class="formField">Select an Artist:</td>
		<td class="dataField"><asp:DropDownList ID="selArtistID" runat="server" CssClass="combobox" DataSourceID="dsArtists" DataTextField="ArtistName" DataValueField="ArtistID" onchange="javascript:window.location.href='ManageArtists.aspx?id=' + this.value;"></asp:DropDownList></td>
	</tr>
	<tr>
		<td class="formField">Artist Name:</td>
		<td class="dataField"><asp:TextBox ID="txtArtistName" runat="server" CssClass="combobox" Width="150px" MaxLength="50"></asp:TextBox></td>
	</tr>
	<tr>
		<td class="formField">Active:</td>
		<td class="dataField"><asp:CheckBox ID="chkActive" runat="server" /></td>
	</tr>
	<tr>
		<td class="formField" valign="top">Image:</td>
		<td class="dataField" colspan="3">
			<asp:FileUpload ID="image1" runat="server" Width="350px" />
			<div id="divCurrentImages" runat="server" style="height:auto;width:500px;padding:10px 10px 10px 10px;background-color:white;border:0px solid #555;"></div>
		</td>
	</tr>
	</table>
	<span class="text4b"><b>Description:</b></span><br />
	<FTB:FreeTextBox id="txtDescription" Width="500px" Height="300px" runat="Server" BreakMode="LineBreak" DesignModeCss="~/style.css"/>
	<div style="margin-top:15px;"><asp:button cssclass="buttonSkinL L" id="btnLogin" Text="Save" onclick="btnSave_Click" runat="server"/></div>
	
	<asp:SqlDataSource ID="dsArtists" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnectionString %>"
		SelectCommand="SELECT 0 AS ArtistID, 'create new artist' AS ArtistName UNION SELECT ArtistID, ArtistName FROM Artists ORDER BY ArtistName">
	</asp:SqlDataSource>
 
	<script language="javascript">
	function LoadImage(src)
	{
		window.open(src);
	}
	function DeleteImage(artistID)
	{
		getHttpRequest("getImages.aspx?guid=<%= Guid.NewGuid().ToString() %>&action=delArtistImage&artistID=" + artistID,"DisplayImages",false,null);
	}
	function DisplayImages()
	{
		retVal = oHttpRequest.responseText.trim();
		document.getElementById("<%= divCurrentImages.ClientID %>").innerHTML = retVal;
	}
	function GetImages()
	{
		if(document.getElementById("<%= hdArtistID.ClientID %>"))
		{
			var artistID = document.getElementById("<%= hdArtistID.ClientID %>").value;
			getHttpRequest("getImages.aspx?guid=<%= Guid.NewGuid().ToString() %>&action=getArtistImage&artistID=" + artistID,"DisplayImages",false,null);
		}
	}
	GetImages();
	</script>
</asp:Content>

Open in new window

0
 
Cedric Obinna A.Full Stack DeveloperCommented:
Line 71 of the code you posted in post with ID 22986674, has the code I pointed out in my previous post. I don't know the name of the file you copied that from but I believe that is where you latest problem comes from.

The code is just above the first one I asked you to correct which you did.
0
 
Sandra_joyAuthor Commented:
Ok I did that and this is what I get:
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1002: ; expected

Source Error:

Line 87: Line 88: string strtest; Line 89: strtest = cmd.ExecuteScalar().ToString();Response.Write "value: " & strtest; Line 90: Line 91: int artistID = Convert.ToInt32((string)cmd.ExecuteScalar());
Source File: d:\Domains\amariahart.com\wwwroot\admin\ManageArtists.aspx.cs    Line: 89


0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 7
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now