on wcf service
public class Response
{
public string ID { get; internal set; }
public string Data { get; set; }
public List<Message> Messages { get; set; }
}
public class Message
{
public DateTime DateTime { get; set; }
public string Message { get; set; }
public string ProcessName { get; set; }
public Message()
{
DateTime = DateTime.Now;
Message = "";
ProcessName = "";
}
}
asp.net
protected void Page_Load(object sender, EventArgs e)
{
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;
EndpointAddress endpoint = new EndpointAddress(new Uri(System.Configuration.ConfigurationManager.AppSettings["wcfserviceURL"]));
Wcfservice.wcfserviceClient wcfservice = new Wcfservice.wcfserviceClient(binding, endpoint);
Wcfservice.Response response = wcfservice.ReturnResponse();
//I need to bind the response.Messages to a listview. It should display data in 3 columns DateTime, Message and ProcessName
}
I used the datasource wizard and it created the following. But I don't know what to do next for the data to be displayed
<asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource1">
</asp:ListView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="Wcfservice.Response" EntityTypeName=""
TableName="Messages">
</asp:LinqDataSource>
Try adding this to your page load, where you're trying to bind the ListView:
LinqDataSource1.DataBind();
Related
Is there a demo (with source) of a datagrid that when we click on a selected row, it opens the details razer page of the selected row?
You are looking for the OnRecordClick parameter. Information can be found here: https://blazor.syncfusion.com/documentation/datagrid/events#rowselecting
#using Syncfusion.Blazor.Grids
<SfGrid DataSource="#Orders">
<GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings>
<GridEvents OnRecordClick="RecordClickHandler" TValue="Order"></GridEvents>
<GridColumns>
<GridColumn Field=#nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
<GridColumn Field=#nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn>
<GridColumn Field=#nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
<GridColumn Field=#nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
</GridColumns>
</SfGrid>
#code{
public List<Order> Orders { get; set; }
protected override void OnInitialized()
{
Orders = Enumerable.Range(1, 75).Select(x => new Order()
{
OrderID = 1000 + x,
CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
Freight = 2.1 * x,
OrderDate = DateTime.Now.AddDays(-x),
}).ToList();
}
public class Order
{
public int? OrderID { get; set; }
public string CustomerID { get; set; }
public DateTime? OrderDate { get; set; }
public double? Freight { get; set; }
}
public void RecordClickHandler(RecordClickEventArgs<Order> args)
{
// Here, you can customize your code.
}
}
Im looping through all the results from the SQL query in a .Net Core project. here is Model
public class Mymessagesinfo
{
public int MyMessagesCount { get; set; }
public List<int> MessagesIDs { get; set; }
public List<int> MessagesSendersid { get; set; }
public List<string> MessagesSenders { get; set; }
public List<string> MessagesTitles { get; set; }
public List<string> MessagesInformation { get; set; }
public List<string> MessagesStatus { get; set; }
}
I loop through the users messages in my controller then i pass that model to the view
sqlcon.Open();
int? userid = HttpContext.Session.GetInt32("UserID");
SqlCommand sqlcom = new SqlCommand("select * from messages where Messagereceiver=" +userid , sqlcon);
SqlDataReader reader = sqlcom.ExecuteReader();
if(reader.HasRows)
{
int index = 0;
while(reader.Read())
{
string s;
s = reader[0].ToString();
Mymessages.MessagesIDs.Add(int.Parse(s));
Mymessages.MessagesSendersid.Add(int.Parse(reader[1].ToString()));
Mymessages.MessagesTitles.Add(reader[3].ToString());
Mymessages.MessagesInformation.Add(reader[4].ToString());
Mymessages.MessagesStatus.Add(reader[5].ToString());
index++;
}
Mymessages.MyMessagesCount = index;
}
the very first line Mymessages.MessagesIDs.Add(int.Parse(s)); it throws an exception saying System.NullReferenceException: 'Object reference not set to an instance of an object
i wanted to make sure that reader was holding the results so i added int s and checked on it and it was holding the value it was supposed to.
whats going wrong here? is this how we are supposed to pass list-like data to the view?
You need to initlize MessagesIDs in entity Mymessages, like this:
var Mymessages = new Mymessagesinfo()
{
MessagesIDs = new List<int>()
};
Mymessages.MessagesIDs.Add(id);
Or just define the class like this,
public class Mymessagesinfo
{
public int MyMessagesCount { get; set; }
public List<int> MessagesIDs { get; set; } = new List<int>();
public List<int> MessagesSendersid { get; set; } = new List<int>();
public List<string> MessagesSenders { get; set; } = new List<string>();
public List<string> MessagesTitles { get; set; } = new List<string>();
public List<string> MessagesInformation { get; set; } = new List<string>();
public List<string> MessagesStatus { get; set; } = new List<string>();
}
Here is how I would restructure what you have to make it work.
First, your model class:
public class Mymessagesinfo
{
public List<MessageInfo> Messages { get; set; } = new List<MessageInfo>();
}
public class MessageInfo
{
public int ID { get; set; }
public int Senderid { get; set; }
public string Sender { get; set; }
public string Title { get; set; }
public string Information { get; set; }
public string Status { get; set; }
}
With this approach you have a list of message objects, instead of a bunch of lists containing property data.
Here is how I would suggest you load it from SQL Server:
var data = new Mymessagesinfo();
int? userid = HttpContext.Session.GetInt32("UserID");
var messagesTable = new System.Data.DataTable("messages");
using (var sqlcom = sqlcon.CreateCommand())
{
sqlcom.CommandText = $"select * from messages where Messagereceiver='{userid}'";
using (var adapter = new SqlDataAdapter(sqcom))
{
adapter.Fill(messagesTable);
}
}
// we are now done with SQL and have the data in memory...
foreach(DataRow row in messagesTable.Rows)
{
data.Messages.Add( new MessageInfo {
ID = row.Field<int>(0),
Senderid = row.Field<int>(1),
Sender = row.Field<string>(2),
Title = row.Field<string>(3),
Information = row.Field<string>(4),
Status = row.Field<string>(5),
});
}
return View(data);
This is a lot cleaner and by using a DataAdapter and DataTable you minimize the amount of time that the connection to the database is connected.
Here is how you would use this model in an MVC View:
#Model Mymessagesinfo
<div>
<!-- This is where you can display the properties of the message. //-->
<ul>
#foreach(var message in Model.Messages)
{
<li> #message.Title - #message.Id </li>
}
<ul>
<div>
i want to get data from database through webapi and show in listview, but with lsitview i want to use paging. I am using syncfusion for listview paging. I have done some part but i am getting error
the error i am getting is 'Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1'
private List<products> products = new List<products>();
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync("http://orangepotato.rjcpacking.com/index.php?route=api/login/getFeaturedProducts");
protected async void FeaturedList()
{ products = JsonConvert.DeserializeObject<List<products>>(json);
dataPager.Source = products;
}
void Handle_OnDemandLoading(object sender, Syncfusion.SfDataGrid.XForms.DataPager.OnDemandLoadingEventArgs e)
{
var source= products.Skip(e.StartIndex).Take(e.PageSize);
FeaturedlistView.ItemsSource = source.AsEnumerable();
}
the json returned by the service is an object containing a list. You cannot deserialize it directly into a List
using json2csharp.com, I get this model for the json
public class Product
{
public string product_id { get; set; }
public string thumb { get; set; }
public string name { get; set; }
public string price { get; set; }
public string special { get; set; }
public int rating { get; set; }
public string discount_percentage { get; set; }
}
public class RootObject
{
public List<Product> products { get; set; }
}
I have the following and I'm not sure how to implement the sorting function on the gridview? The data and paging work fine. Just not sure how to handle the sorting method? I simply want to sort columns by asc and desc.
public partial class inLinksAuthGrid : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterAsyncTask(new PageAsyncTask(LoadSomeData));
}
public async Task LoadSomeData()
{
try
{
var client = new WebClient();
client.Credentials = new NetworkCredential("test", "test");
var myInLinks = client.DownloadStringTaskAsync("http://inlink-xxxxxx.net:5000/inlinks");
await Task.WhenAll(myInLinks);
var links = JsonConvert.DeserializeObject<Rootobject>(await myInLinks);
GridView1.DataSource = links.inlinks;
GridView1.DataBind();
}
catch (Exception ex)
{
//TODO:
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
// WHAT do I do here?
}
public class Rootobject
{
public Inlink[] inlinks { get; set; }
}
public class Inlink
{
public string clicks { get; set; }
public string email { get; set; }
public string ip { get; set; }
public string keyword { get; set; }
public string timestamp { get; set; }
public string title { get; set; }
public string url { get; set; }
}
}
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="900" OnPageIndexChanging="GridView1_PageIndexChanging" OnSorting="GridView1_Sorting" AllowSorting="True"></asp:GridView>
Everything works just have no idea how to get sorting to work?
Thanks!
Set the allowsorting property of the gridview to true like allowsorting="true" since you are trying to sort the column by asc/desc. See GridView.AllowSorting Property
I want to add a textBox in GridView knowing that the textbox retrieves data from a collection like ArrayList or List(OF ..) not from a dataSource (ASP.net)
Set the DataSource in code:
// in this case the object I am using is:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public bool IsInStock { get; set; }
}
private void BindGrid()
{
// in the GetProducts() method return the collection
List<Product> r = GetProducts();
this.grdSomething.DataSource = r;
this.grdSomething.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
this.BindGrid();
}
protected void grdSomething_EditCommand(object sender, DataGridCommandEventArgs e)
{
this.grdSomething.EditItemIndex = e.Item.ItemIndex;
this.BindGrid();
}
Simply add a bound field column like this:
<asp:DataGrid runat="server" ID="grdSomething" AutoGenerateColumns="False"
OnEditCommand="grdSomething_EditCommand">
<Columns>
<asp:BoundColumn DataField="Name"></asp:BoundColumn>
<asp:EditCommandColumn EditText="Edit"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
That's it