I'm trying to use a cookie to store an integer value for a switch.
private int LessonSlideNum;
I get:
FormatException Input string was not in a correct format
on this line:
LessonSlideNum = Convert.ToInt32(testCookie.Value);
or this one:
LessonSlideNum = Int.Parse(testCookie.Value);
when I use this code:
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie testCookie = Request.Cookies["TestCookie"];
if (testCookie == null)
{
testCookie = new HttpCookie("TestCookie");
testCookie.Path = "~/App_Data/Cookies";
Response.Cookies.Add(testCookie);
}
else
{
LessonSlideNum = Convert.ToInt32(testCookie.Value);
}
Response.Cookies.Add(testCookie);
}
It seems that VS is expecting something in DateTime format. Could someone please help me out here? Thanks :)
The error was that I had no values in the actual cookie. Thanks Ali Shahrokhi! Still, I find it strange that the line:
if(testCookie == null)
didn't catch this error...
Related
I'm pretty new to C# and MVC coding. I'm making a booking application and I'm using DayPilotMonth for a calendar. I'm using the following code in my view:
#Html.DayPilotMonth("dpm", new DayPilotMonthConfig
{
BackendUrl = Url.Content("~/Home/Backend"),
TimeRangeSelectedHandling = DayPilot.Web.Mvc.Events.Month.TimeRangeSelectedHandlingType.JavaScript,
TimeRangeSelectedJavaScript = "document.location='Bokabord2?startingtime={0}';"})
According to someone on DayPilot the {0} in TimeRangeSelectedJavaScript gives the clicked date in DateTime format.
Now when I'm trying to use the 'startingtime' in my controller nothing works. It seems like the value is always null and I can't find a way to convert it. This is the code I have been trying to use:
public ActionResult Bokabord2(DateTime? startingtime)
{
DateTime startingTime;
if (Session["InloggatId"] != null)
{
if (Request.QueryString["startingtime"] != null)
{
startingTime = Convert.ToDateTime(Request.QueryString["startingtime"]);
ViewBag.Message2 = startingtime;
}
else
{
ViewBag.Message2 = "Error";
}
return View();
}
else
{
return RedirectToAction("Login", "Account");
}
}
When I run the application the error message always shows up at the Convert.ToDateTime line and says something about string not able to convert to DateTime.
Thanks in advance
Regards Philip
protected void builtyLinkButton_click(object sender, EventArgs e)
{
LinkButton lnk = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnk.NamingContainer;
Label l1 = (Label)row.FindControl("Lbl_id");
Response.Redirect("Loading_request.aspx?Id=" + (l1.Text) + "&Date=" + (DateTime.Now.ToString()));
}
I just got it to work. The problem was in the View.
I had to change
TimeRangeSelectedJavaScript = "document.location='Bokabord2?startingtime={0}';"
To:
TimeRangeSelectedJavaScript = "document.location='Bokabord2?startingtime=' + start;"
Now it sends a string with the selected date to the controller. There is very little information about the functions of DayPilotMonth on their website, hence why I couldn't find anything about it. Thanks all.
I'm trying to store a value that is an int into a table, but I keep getting the message "cannot implicitly convert string to int" if I change the table datatype from int32 to string then I get this message http://postimg.org/image/cv1cc4jkf/full/
Can anyone help me resolve this issue? easyScoreLabel, mediumScoreLabel, and highScoreLabel are labels I dragged onto the web application from the toolbox.
protected void myScoresButton_Click(object sender, EventArgs e)
{
using (projectDBEntities1 dbcontext = new projectDBEntities1())
{
message aMessage = new message();
aMessage.userName = nameTextBox.Text;
aMessage.highScoreEasy = Int32.Parse(easyScoreLabel.Text);
aMessage.highScoreMedium = Int32.Parse(mediumScoreLabel.Text);
aMessage.highScoreHard = Int32.Parse(hardScoreLabel.Text);
dbcontext.messages.Add(aMessage);
dbcontext.SaveChanges();
}
GridView1.DataBind();
}
easyScoreLabel.Text is the string. You need to convert it to the int.
aMessage.highScoreEasy = Int.Parse(easyScoreLabel.Text);
aMessage.highScoreMedium = Int.Parse(mediumScoreLabel.Text);
aMessage.highScoreHard = Int.Parse(hardScoreLabel.Text);
I am using android SDK with Microsoft VS2010 C#. I want to use string values from my /resources/values/strings file in my C# code. Here is a piece of code that illustrates what I want to do. I am not getting the string value. I know that the resource id is an int, but what I need is the actual string value behind that id.
void vx2OkButton_Click(object sender, EventArgs e)
{
Log.Info(LOG_TAG, "Hello from OkButton|Enter()button"); // also used as Enter button
strVx20kButtonText = vx2OkButton.Text.ToString();
mDwnLdCodeEnteredByUser = vxxDwnldCodeEntered.Text.Trim();
string strDwnldCodeOut = mActCode.Bad.ToString();
if(strVx20kButtonText == Resource.String.Enter.ToString())
{
if (mDwnLdCodeEnteredByUser.Length < 1)
{
vxxSystemMsgBox.SetText(Resource.String.FieldRequried_);
m_txvEnterDwnLdCode.SetTextAppearance(this,Resource.Color.Red);
return;
}
// verify the dwnldcodeenter by the user matches the assigned to user when at the time the downloaded the app
mDwnLoadStatus = VerifyDwnLoadCode(mDwnLdCodeEnteredByUser);
if (mDwnLoadStatus == mDwnLdStatCode.BadDwnLdCode.ToString())
{
vxxSystemMsgBox.SetText(Resource.String.InvalidValueEntered);
m_txvEnterDwnLdCode.SetTextAppearance(this, Resource.Color.Red);
return;
}
mActionCD = mActCode.Ok.ToString();
vx2OkButton.SetText(Resource.String.OkButtonText);
vxxSystemMsgBox.SetText(Resource.String.ThanksPressOkButton);
m_txvEnterDwnLdCode.SetTextAppearance(this,Resource.Color.White);
return;
}
As you noticed, Resource.String.Enter is an integer generated for you that you can use to access the string resource. You can access it using the Android.Content.Res.Resources.GetString() method:
string enter = Resources.GetString(Resource.String.Enter);
I´m trying to use session to store a value (id). The problem is that I have to store it as a string. When trying to use the instance of the id I get the error:
Exception Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
Line 156:
Line 157: Nemanet_Navigation newFile = new Nemanet_Navigation();
Line 158: newFile.Nav_pID = (Guid)Session["id"];
Line 159:
Line 160:
This is where I get the id and that seems to work fine. Session["id"] gets the value.
public void treeview_Navigation_SelectedNodeChanged(object sender, EventArgs e)
{
TreeNode node = treeview_Navigation.FindNode(treeview_Navigation.SelectedNode.ValuePath);
NavTreeNode nNode = node as NavTreeNode;
Session["id"]=((TreeView)sender).SelectedValue.ToString();
}
But this code does not seem to work. I get the error mentioned above.
protected void Button1_Click(object sender, EventArgs e)
{
Nemanet_Navigation newFile = new Nemanet_Navigation();
newFile.Nav_pID = (Guid)Session["id"];
}
Use Guid.Parse
protected void Button1_Click(object sender, EventArgs e)
{
Nemanet_Navigation newFile = new Nemanet_Navigation();
newFile.Nav_pID = Guid.Parse(Session["id"] as string);
}
Try using:
newFile.Nav_pID = new Guid(Session["id"].ToString());
You are converting GUID to string before saving it in session collection. But when you are retrieving it, you are trying to cast it in GUID which is invalid. String and GUID are not compatible types. Either save it as GUID or convert it into string (like you are doing) when saving and use GUID constructor that takes string to reform GUID instance.
You can do it like this:
Session["id"]=((TreeView)sender).SelectedValue.ToString();
and then retrieve it from session like:
newFile.Nav_pID = new Guid((string)Session["id"]);
By default the SessionID created by either aspx is not a GUID. You can created your own data type session id value. e.g :
HttpCookie myCookie = new HttpCookie("UserSettings");
myCookie.Expires = DateTime.Now.AddMinutes(1.0);
myCookie.Value = g1.ToString();
I am using C#. Below is my sample code.
private void Page_Load(object sender, System.EventArgs e)
{
string str = Request.UrlReferrer.ToString();
Label1.Text = str;
}
The result in Label1.Text is http://localhost:82/data/WebForm1.aspx.
Now I want the result "WebForm1.aspx" in Label1.Text
can you please help me?
Thanks.
If you want only the part after the last / in the URL, calling the System.IO.Path.GetFileName() method on the Uri.LocalPath should do the trick:
System.IO.Path.GetFileName(Request.UrlReferrer.LocalPath);
If you want the output to keep query string information from the URI, use the PathAndQuery property:
System.IO.Path.GetFileName(Request.UrlReferrer.PathAndQuery);
Try the LocalPath property on the UrlReferrer:
Label1.Text = Request.UrlReferrer.LocalPath;
It should provide you with just the filename.
Edit: this seems to also include the path, so only works for root.
In which case, you're better off just using Substring():
string str = Request.UrlReferrer.ToString();
Label1.Text = str.Substring(str.LastIndexOf('/')+1);