Error is shown in function - c#

What is causing an error in my code below:
public void SetOperationDropDown()
{
if(CmbOperations.Items.Count == 0)
{
//ByDefault the selected text in the cmbOperations will be -SELECT OPERATIONS-.
cmbOperations.SelectedItem = "-SELECT OPERATIONS-";
//This is for adding four operations with value in operation dropdown
cmbOperations.Items.Insert(0, "PrimaryKeyTables");
cmbOperations.Items.Insert(1, "NonPrimaryKeyTables");
cmbOperations.Items.Insert(2, "ForeignKeyTables");
cmbOperations.Items.Insert(3, "NonForeignKeyTables");
cmbOperations.Items.Insert(4, "UPPERCASEDTables");
cmbOperations.Items.Insert(5, "lowercasedtables");
}
else
{
int? cbSelectedValue = null;
//OP ERROR SHOWN HERE
if(!string.IsNullOrEmpty(cmbOperations.SelectedValue))
cbSelectedValue = convert.toInt32(cmbOperations.SelectedValue);
}
//load your combo again
//OP ERROR SHOWN HERE
if(cbSelectedValue != null)
cmbOperations.SelectedValue = cbSelectedValue.ToString();
}
(The error occurs specifically in this snippet of code:
cbSelectedValue.ToString();)

SelectedValue is an object, not a string. So you need to convert it to a string:
if(cmbOperations.SelectedValue != null && !cmbOperations.SelectedValue.ToString() == string.Empty)

Related

C# VSTO word AddIn Throws value does not fall within expected error

I am new to VSTO word Addin,the end goal is to check if the custom document property exists.Read all the available articles online with no breakthrough.
Started of with this code
public void chk()
{
if (this.Application.ActiveDocument.CustomDocumentProperties["ObjectType"].Value = 0)
{
MessageBox.Show("Please select an object");
}
}
This throws the error
Value does not fall within bounds error
Modified the code to create a custom document property object as below and it gives me the same error on
if(this.Application.ActiveDocument.CustomDocumentProperties["ObjectType"].Value
= 0)
public void chk()
{
dynamic properties = null;
properties = this.Application.ActiveDocument.CustomDocumentProperties;
properties.Add("Name",false,Office.MsoDocProperties.msoPropertyTypeString, "ObjectType");
properties.Add("LinkToContent",false, Office.MsoDocProperties.msoPropertyTypeBoolean,false);
properties.Add("Type",false,Office.MsoDocProperties.msoPropertyTypeNumber, 0);
if(this.Application.ActiveDocument.CustomDocumentProperties["ObjectType"].Value = 0)
{
MessageBox.Show("Please select an object");
}
}
Requesting you to suggest the steps to fix the issue. Please let me know if more information is required.
You can iterate over all properties and check their names, compare two approaches:
void TestProperties()
{
Microsoft.Office.Core.DocumentProperties properties;
properties = (Office.DocumentProperties)this.CustomDocumentProperties;
if (ReadDocumentProperty("Project Name") != null)
{
properties["Project Name"].Delete();
}
properties.Add("Project Name", false,
Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
"White Papers");
}
private string ReadDocumentProperty(string propertyName)
{
Office.DocumentProperties properties;
properties = (Office.DocumentProperties)this.CustomDocumentProperties;
foreach (Office.DocumentProperty prop in properties)
{
if (prop.Name == propertyName)
{
return prop.Value.ToString();
}
}
return null;
}

How to solve this error : must specify valid information for parsing in the string

I want to convert a string to enum . My code is like this
var categoryEnum = (SiteCategory) Enum.Parse(typeof (SiteCategory), UIController.QueryStringParamInitialValue, true);
And QueryStringParamInitialValue() is like this
public static string QueryStringParamInitialValue
{
get
{
string str = string.Empty;
if (HttpContext.Current != null && HttpContext.Current.Session != null && HttpContext.Current.Session["UIController_QueryStringParamInitialValue"] != null)
str = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Session["UIController_QueryStringParamInitialValue"] as string);
return str;
}
set
{
HttpContext.Current.Session["UIController_QueryStringParamInitialValue"] = (object)HttpContext.Current.Server.UrlEncode(value);
}
}
I am facing error in var categoryEnum = (SiteCategory) Enum.Parse(typeof (SiteCategory), UIController.QueryStringParamInitialValue, true);
Any information regarding this will be helpful..pls help
If seeing this error as a part of a Xamarin project:
As mentioned here I had to "launch xCode at least one time after it's update." for this error to go away. I also had to restart Visual Studio after the xCode update and launch.

Can't get a correct DateTime from querystring

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.

webbrowser with emoji / emoticon

Folks,
I'm doing a post on a web page with emoji / emoticon. But after posted the site does not display the emoticon. Must you use a different Encoding? If so how can I do?
Example have this emoji 👐💓⛪🌇 the site only shows me that ⛪ Other special characters appear.
if (currentElement.GetAttribute("type") == "submit")
if (currentElement.Name == "view_post")
{
string postagem = txtPublicacao.Text;
HtmlElement elea = webBrowser1.Document.GetElementById("u_0_0");
if (elea != null)
elea.SetAttribute("value", postagem);
currentElement.InvokeMember("click");
}
I think you can prevent yourself from experiencing some future grief by ensuring that blocks are enclosed in brackets like so:
if (currentElement.GetAttribute("type") == "submit")
{
if (currentElement.Name == "view_post")
{
string postagem = txtPublicacao.Text;
HtmlElement elea = webBrowser1.Document.GetElementById("u_0_0");
// if condition and response either on one line:
if (elea != null) elea.SetAttribute("value", postagem);
// ...or use "{}" in preparation for possible future additions to the reponse to the if condition
if (elea != null)
{
elea.SetAttribute("value", postagem);
}
currentElement.InvokeMember("click");
}
}
Or better yet, since you have two consecutive "ifs" before code is executed, combine them like so:
if ((currentElement.GetAttribute("type") == "submit") &&
(currentElement.Name == "view_post"))
{
string postagem = txtPublicacao.Text;
HtmlElement elea = webBrowser1.Document.GetElementById("u_0_0");
if (elea != null) elea.SetAttribute("value", postagem);
currentElement.InvokeMember("click");
}

Getting null value for NotesDocument for Mail Item : C#

I am accessing From value of each mail from nsf file.
As:
NotesView sent = _NotesDatabase.GetView("($Sent)");
if (sent != null)
{
NotesDocument docSent = sent.GetFirstDocument();
if (docSent != null)
{
while (docSent != null)
{
String Subject = ( (object[]) DocSent.GetItemValue("Subject"))[0] as String;
Message.Show(Subject);
docSent = sent.GetNextDocument(docSent);
}//while
}
}
But there are some mails for which i am getting "null" value (it contains SendTo,Subject e.t.c values: viewed in lotus notes).
So i can't access Subject of it.
Why it is happening?
i checked Form value it is "Memo"
If you're getting a null value from GetItemValue, then the field is probably not on the document. You can check for this condition using the HasItem method of the NotesDocument class, as in:
if (docSent.hasItem("Subject")) {
...
}

Categories