HttpContext.Session.SetString("module", "CallLogDetail");
HttpContext.Session.SetString("module", "CommentsPanel");
HttpContext.Session.SetString("module", "Update");
HttpContext.Session.SetString("Id", "0");
return View();
i have these code of line here Id is string how can i now change it to int.
If you want to set Id to int instead of string in session, You can use .SetInt32(string key,int value) to achieve it.
Please refer to this Microsoft Docs.
Related
Basically I have a few asp.net objects. I get the ID of object 1 and assign it to a string. I then want to add a control to a placeholder than end in that ID. Not too sure how to go about doing this.
string id = Regex.Match(btnCreateHazard.ID, #"\d+$").Value;
phHazard(SOMEHOW GET THE ID HERE).Controls.Add(txtHazardDesc);
Thanks a lot.
What i understood id you want to add a string to a control that u can do like this..
string id = Regex.Match(btnCreateHazard.ID, #"\d+$").Value;
placeholderId.Controls.Add(id);
OR
can refer below example.
https://msdn.microsoft.com/en-us/library/kyt0fzt1.aspx
I am currently using the following script to set this value as a String:
string ID = Request.QueryString["ID"].ToString();
However, I'd now like to store it as an Integer.
How do I do this?
Many thanks for any pointers.
Assuming you don't want to throw a server error on a bad string
int id=0;
if (int.TryParse(Request.QueryString["ID"],out id)) {
.. logic for valid id
} else {
.. logic for invalid id
}
int ID = int.Parse(Request.QueryString["ID"].ToString());
Use either of these:
If you know that you have an ID:
string ID = Request.QueryString["ID"];
int integerId = int.Parse(ID);
or, if the query string may be missing or invalid (never trust query strings....)
string ID = Request.QueryString["ID"];
int integerId;
if (int.TryParse(ID, out integerId))
{
// you have a valid integer ID here.
// process it
}
else
{
// handle missing or invalid ID
}
You could do something like:
int i = Convert.ToInt32(ID);
or
int i;
Int32.TryParse(ID, out i);
BTW Request.QueryString["ID"] is already a string so the following is fine:
string ID = Request.QueryString["ID"];
Try
int ID = int.Parse(Request.QueryString["ID"]);
See How can I convert String to Int?
You can do like this:
string ID = Request.QueryString["ID"].ToString();
int id=int.Parse(ID);
or
int id=Convert.ToInt16(ID);
Always use tryparse for querystring values if you want to convert it to integer even if you never set it to string , because user can change that anytime before sending request (visible in URL).
int id = 0 ;//default value
bool success = int.TryParse(Request.QueryString["ID"],out id))
if (success) {
//write code for default value action
return;
}
//write code for other values.
I am at the end of my knowledge and googled for the answer too but no luck :/
Week ago everything worked well.
I did a revert on the repository, recreated the tableadapter etc... nothing helped.
When I try to save in my application I get an SystemInvalidCastException at this point:
PersonListDataSet.cs:
partial class P_GroupTableAdapter
{
public int Update(PersonListDataSet.P_GroupDataTable dataTable, string userId)
{
this.Adapter.InsertCommand.Parameters["#userId"].Value = userId;
this.Adapter.DeleteCommand.Parameters["#userId"].Value = userId;
this.Adapter.UpdateCommand.Parameters["#userId"].Value = userId;
return this.Update(dataTable); **<-- Exception occurs here**
}
}
Everything is stuck here because a Guid - and I checked the datatable preview with the magnifier tool its really a true Guid in the column of the datatable - can not be converted to a string ??? How can that happen?
It's the other way around. Your userId is a string and you need a GUID value for your parameters:
Parameters["#userId"].Value = new Guid(userId);
Provided UserId is in one of the supported formats for a GUID. The constructor supports many formats.
Edit, based on comments below:
It turns out that you are asking how to run a select statement like:
SELECT ....
WHERE '{BB6DFF45-FDA7-4155-86D0-0CBF129A9104}' = `domainname\\jondoe`
I think you should re-check your datamodel and find a solution.
Have you tried:
this.Adapter.InsertCommand.Parameters["#userId"].Value = new Guid(userId);
this.Adapter.DeleteCommand.Parameters["#userId"].Value = new Guid(userId);
this.Adapter.UpdateCommand.Parameters["#userId"].Value = new Guid(userId);
Hope it helps!!!
I have a question about converting types. I want to change the currently selected combobox value string to an int, but I get errors
My code:
int.Parse(age.SelectedItem.ToString());
What can I do for this problem?
Ok now we know the error, you can check for a null value before trying to parse it using:
if (comboBox1.SelectedItem != null)
{
int x = int.Parse(comboBox1.SelectedItem.ToString());
}
else { //Value is null }
You can avoid a null value being passed by setting the text property of the control to what ever default value you want.
If you are still not getting a value after making sure one is selected you really need to post your code.
TryParse is a good method for this sort of thing:
int value;
if (!Int32.TryParse(this.comboBoxNumeric.Text, out value))
{
//Do something fun...
}
Use a Convert.ToInt32 method. You can always use the databinding like this:
class A
{
public int ID{get;set;}
public string Name{get;set;}
}
cbo.DataSource = new A[]{new A{ID=1, Name="hello"}};
cbo.DisplayMember = "Name";
cbo.DisplayValue = "ID";
int id = Convert.ToInt32(cbo.SelectedValue);
A a = (A) cbo.SelectedItem;
int a_id = a.ID;
int a_name = a.Name;
If you use LINQ to DataSets, develop is very easy for C# program.
try
{
string name = comboBoxPort.SelectedItem.ToString();
int portBaudrate = Convert.ToInt32(comboBoxBaudrate.SelectedItem);
}
//just solved
//enjoy
I have this user control:
<user:RatingStars runat="server" product="<%= getProductId() %>" category="<%= getCategoryId() %>"></user:RatingStars>
You can see that I fill in the product and category by calling two methods:
public string getProductId()
{
return productId.ToString();
}
public string getCategoryId()
{
return categoryId.ToString();
}
I do not understand why, in the user control, when I take the data received (product and category) it gives me "<%= getProductId() %>" instead of giving the id received from that method...
Any help would be kindly appreciated...
Edit: Solved with: product='<%# getProductId() %>'
Last problem: in the user control I have this:
public string productId;
public string product
{
get
{
return productId;
}
set
{
productId = value;
}
}
So, I expect that the productId is set up ok in the user control.
Unfortunately it is null when I try to use it...
Is there anything I wrote that's incorrect?
So that you get compile-time checking, you can give your user control an ID and then set its Product and Category properties in C# like this:
ASPX:
<user:RatingStars id="myUserControlID" runat="server" Product="<%= getProductId() %>" Category="<%= getCategoryId() %>"></user:RatingStars>
CS:
myUserControlID.Product = GetProductId();
myUserControlID.Category = GetCategoryId();
Also, as 5arx mentions, once you've populated that then refreshing your page will reload your control and you'll lose the Product and Category IDs. You can handle that by using ViewState on the properties in your user control, like this:
private const string ProductKey = "ProductViewStateKey";
public string Product
{
get
{
if (ViewState[ProductKey] == null)
{
// do whatever you want here in case it's null
// throw an error, return string.empty or whatever
}
return ViewState[ProductKey].ToString();
}
set
{
ViewState[ProductKey] = value;
}
}
NOTE: I've updated the property name casing to follow convention, as it just makes more sense to me that way! Personally, I'd always suffix IDs with ID (eg: ProductID) to distinguish it from a property that contains a Product object. Read more about coding standards here: Are there any suggestions for developing a C# coding standards / best practices document?
Please post some more of your code?
A couple of things:
productId is an object reference (to a string object) which means:
unless you write some intialisation code to 'fill' it with a string reference at the start, it will be null.
if you create a string e.g. string x = new String() x will be an empty string, which you can think of as "" without the quotes. (It is a string, but its empty because it has no characters in it).
you can just write return productId; - no need to call ToString() on a string.
Your page does not have an out-of-the-box mechanism to store variables across postbacks. You need to use ViewState or hidden form fields to do this.