How to bind data in gridview in asp.net without using properties - asp.net

I have this code I want to bind data to grid view without using properties .
public class UserTerritory
{
public string TerrId;
public string TerrName;
public string AccAccessLevel;
public UserTerritory(string _TerrId, string _TerrName, string _AccAccessLevel)
{
this.TerrId = _TerrId;
this.TerrName = _TerrName;
this.AccAccessLevel= _AccAccessLevel;
}
}
protected void Page_Load(object sender, EventArgs e)
{
List<UserTerritory> ut = new List<UserTerritory>();
ut.Add(new UserTerritory("1", "x", "a"));
ut.Add(new UserTerritory("2", "y", "b"));
ut.Add(new UserTerritory("3", "z", "c"));
grdUserTerr.DataSource = ut;
grdUserTerr.DataBind();
}
When I execute the above code I get following Httpexception " The data source for GridView with id 'grdUserTerr' did not have any properties or attributes from which to generate columns. Ensure that your data source has content."
Can Somebody tell me what wrong I am doing ?
Thanks for your reply

The problem is that databinding uses DataBinder.Eval (or just Eval ) behind the scenes and it
looks only for properties. They aren't exactly the same as public members (fields). Properties are closer to methods than fields are.
So why don't you just use properties like this?
public string AccAccessLevel { get; set; }
public string TerrName { get; set; }
public string TerrId { get; set; }

there is problem some where in the property decalration section please change the properties as private memeber it will works for you.
public class UserTerritory
{
private string TerrId;
private string TerrName;
private string AccAccessLevel;
public UserTerritory(string _TerrId, string _TerrName, string _AccAccessLevel)
{
this.TerrId = _TerrId;
this.TerrName = _TerrName;
this.AccAccessLevel= _AccAccessLevel;
}
}

Related

How to safe a ObservableCollection within a ObservableCollection in xamarin.forms with Sqlite.net

Last 3 days i tried to save my collection in my database.
I tried it in all sorts of ways... without success.
The last one i tried is to serialize my collection and save it to a byte[] everytime the collection changes.
And on appstart i tried to derserialize it in my collection.
I'm just trying to show the most important thing's.
I can save and reload this class without problems from my database.
[Serializable]
public class MainElement : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public static int count = 0;
[PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
public byte[] _elements;
private ObservableCollection<SecondElement> elements;
[OneToMany]
public ObservableCollection<SecondElement> Elements
{
get
{
return elements;
}
set
{
elements = value;
this.SerializeElementsFromMainElement();
//_elements = ExtensionAndDb.SerializeElementsFromMainElement(elements);
OnPropertyChanged();
}
}
public MainElement()
{
Id = count++;
Elements = new ObservableCollection<SecondElement>();
this.DeSerializeElementsFromMainElement();
//Elements = ExtensionAndDb.DeSerializeElementsFromMainElement(_elements);
}
public void OnPropertyChanged([CallerMemberName] string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
public void DeSerializeElementsFromMainElement()
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream(_elements);
using (stream)
{
Elements = bf.Deserialize(stream) as ObservableCollection<SecondElement>;
}
}
public void SerializeElementsFromMainElement()
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
using (stream)
{
bf.Serialize(stream, Elements);
_elements = stream.ToArray();
}
}
}
Every MainElement is stored in the MainList (ObservableCollection).
Everything is fine.
But i cant save or reload the public byte[] _elements; from/to the collection.
I know my code is not good, but if anyone has an idea or can show me how to save the public ObservableCollection<SecondElement> Elements with my class together in Sqlite, i would be really grateful.
I've been trying for 3 days
if you need more code, let me know.
Thank's in advance.
According to your description, you want to serialize and deserialie ObservableCollection, I suggest you ca use Newtonsoft.Json to do this.
Installing Newtonsoft.Json by Nuget package firstly.
For example, I do one esample:
public class person
{
public string username { get; set; }
public int age { get; set; }
}
public ObservableCollection<person> persons { get; set; }
private string list;
public Page13()
{
InitializeComponent();
persons = new ObservableCollection<person>()
{
new person(){username="cherry",age=12},
new person(){username="barry",age=14}
};
}
Btn1.clicked is to serialize data, Btn2.clicked is to deserialize data.
private void Btn1_Clicked(object sender, EventArgs e)
{
list = Newtonsoft.Json.JsonConvert.SerializeObject(persons);
}
private void Btn2_Clicked(object sender, EventArgs e)
{
var myValue = Newtonsoft.Json.JsonConvert.DeserializeObject<ObservableCollection<person>>(list) ;
}
If the Observablecollection changed, you can query this data by Id or other, then update this data.
Here is the article about Update, insert and save data in sqlite, you can take a look:
https://dzone.com/articles/register-and-login-using-sqlite-in-xamarinforms

How to use Json passing by hiddenfield to replace the way of using viewstate in GridView in ASP web form

My project is ASP web forms, and I recently hope to use json.net to replace the viewstate which I use in gridview, like following:
[Serializable]
public class MyDataForm
{
public string LocationCode { get; set; }
public string ProjectNo { get; set; }
public string PatientName { get; set; }
public string PersonID { get; set; }
public string PdfURL { get; set; }
public string Memos { get; set; }
}
public List<MyDataForm> MyDataList { set { ViewState["MyDataList"] = value; } get { return ViewState["MyDataList"] as List<MyDataForm>; } }
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = MyDataList;
GridView1.DataBind();
}
I hope to store the data in HiddenField, and then catch the data back via this when post back each time:
JsonConvert.DeserializeObject<MyDataForm>( $("#hiddenFieldID").val())
The main problem is come from when to replace viewstate in ASP page life cycle.
In which Asp page life cycle stage, like Page_init, the gridview will call databind(), and if I can put the data which deserialize by Json in datasource,
and then I think it can replace viewstate completely, is anyone know how to make that, thanks a lot.
After doing many research, I found there is no way to using Json passing by hiddenfield to replace ViewState using in gridview. The main reason is the hiddenfield in ASP is not designed to send mass data.So, once the data is big enough, it will be slow down, no matter what library I use to parsing string in hiddenfield.

pass the data of GridViewRow of one class to another class

I've a class "get_User.cs" inside "UI" folder. I am using GridView to display data of User.
Now I've to pass the data of GridViewRow to another class "ClsUserRecords.cs".
This "ClsUserRecords.cs" is inside folder AppCode -> BusinessObjects -> "ClsUserRecords.cs". how can I do this?
My current code ::
// get_User.cs
private ClsUserRecords.UserRecords SetEmpRecord(GridViewRow selectedRow, string userName)
{
ClsUserRecords.UserRecords obj = new ClsUserRecords.UserRecords();
obj.user_id = CleanText(((HiddenField)selectedRow.FindControl("hidden_user_id")).Value);
obj.user_name = userName;
obj.user_address = CleanText(selectedRow.Cells[12].Text);
return obj;
}
// ClsUserRecords.cs
public class UserRecords
{
public string user_id { get; set; }
public string user_address { get; set; }
public string user_name { get; set; }
}
// do my stuff
Now I want to change the approach. I want to pass the value of GridViewRow without calling the class ClsUserRecords inside get_User. But calling the GridViewRow from the class ClsUserRecords. How can I do this, is it possible or not?

pass variable to the user control

I have created an user control for cascading two dropdownlists "province" and "city". But everytime when I run it, the error message said "An unhandled exception of type 'System.StackOverflowException' occurred"
Here is my user control code
public partial class cascadingdropdownlist : System.Web.UI.UserControl
{
public string province_selectedvalue
{
set
{
string province_selectedvalue = value;
}
get
{
return city_selectedvalue;
}
}
public string city_selectedvalue
{
set
{
string city_selectedvalue = value;
}
get
{
return city_selectedvalue;
}
}
protected void Page_Load(object sender, EventArgs e)
{
string a = province_selectedvalue;
............
}
}
In the host page
<uc1:cascadingdropdownlist ID="province_city" runat="server" OnPreLoad="province_city_OnPreLoad"/>
The code behind is
protected void province_city_OnPreLoad(object sender, EventArgs e)
{
province_city.province_selectedvalue = myReader["Province/State"].ToString();
province_city.city_selectedvalue = myReader["City"].ToString();
}
The error happened in calling user control the province_selectedvalue.get method. why I don't understand why? Anyone can help me, thanks very much
The problem is in your property:
public string city_selectedvalue
{
set
{
string city_selectedvalue = value;
}
Calls the setter (or the getter!) on city_selectedvalue over and over again resulting in a stack overflow.
Replace your property with this
public string city_selectedvalue { get; set; }
I also thought you could do the same with province_selectedvalue but its getter refers to city_selectedvalue - is that correct?
You are getting that error because the code is in an infinitive loop. When you call province_selectedvalue.get you are calling city_selectedvalue get. And you have the same name for the property in the variable you think you are calling so it is calling the same over and over again.
Change you code to
public string city_selectedvalue { get; set; }
And
public string province_selectedvalue { get; set; }

ASP:RadioButtonList Databind Custom Object

Hey guys, I'm having a bit of trouble with my ASP:RadioButtonList, searched Google and SO, no luck, can you help me out?
I'm having trouble databinding. I've got a custom class that looks like this:
public class myClass{
public myInnerClass{
public int myID;
public String myTextField;
/* other fields*/
}
public List<myInnerClass> myList;
}
And I'm trying to bind a Generic List of it's inner class to a radiolist:
protected void Page_Load(object sender, EventArgs e){
myClass data = anotherClass.getData();
uxRadioList1.DataSource = data.myList;
uxRadioList1.DataTextField = "myTextField";
uxRadioList1.DataValueField = "myID";
uxRadioList1.DataBind();
}
But it just won't go. When I don't specify the DataTextField and DataValueField field it binds, but it displays 'myClass+myInnerClass' . How do I do this properly?
I think you can only bind to public properties, but not to fields. Try changing the fields of myInnerClass to properties:
public class myClass{
public myInnerClass{
public int myID { get; set; }
public String myTextField { get; set; }
/* other fields*/
}
public List<myInnerClass> myList;
}

Resources