Error in ASP.NET C# (implicit conversion) - asp.net

I am new in ASP.NET programming. Please help me.
void DisplayData()
{
DataTable dt = new DataTable();
//objBuyer.BuyerId = Convert.ToInt64(Request.QueryString["id"]);
**ERROR-->>>** dt = objBuyer.DisplayData();********
if (dt.Rows.Count > 0)
{
txtBName.Text = dt.Rows[0][1].ToString();
ERROR:Cannot implicitly convert type 'void' to 'System.Data.DataTable'

You are trying to convert void to a DataTable, which is not possible. Your method has to return a DataTable for this to work.

The problem is that the DisplayData method does not return a DataTable object, it simply displays the data in objBuyer, and returns void.
That's the problem, but I can't really help further than that without some sort of context!

The DisplayData() method needs to return a DataTable in order for this to work.
A nice and easy tutorial can be found here: http://www.aspnettutorials.com/tutorials/controls/data-table-csharp.aspx
As you can see, there's a DataTable created and after that's done, several rows are added to the table with the Rows.Add() method.

Related

gridview is not showing data properly

while trying to get data from https://blockchain.info/ticker in my grid view, i'm finding the result in single coloumn whether i want it in different coloumn.
i'm showing you my code and please give me a soloution.
public void ticker()
{
WebClient webClient = new WebClient();
dynamic result =webClient.DownloadString("https://blockchain.info/ticker");
GridView1.DataSource = result;
GridView1.DataBind();
}
[https://i.stack.imgur.com/nPjgS.jpg]
It looks like your results in JSON format so you have to convert in the proper format to display well in GridView.
import Newtonsoft.Json
dynamic result =JsonConvert.DeserializeObject(
webClient.DownloadString("https://blockchain.info/ticker"));
I hope it's help you, you can search more about it
thanks

System.OutOfMemoryException with JSON.NET

Tried this with JSON.NET 6.0
DataSet ds = Newtonsoft.Json.JsonConvert.DeserializeObject<DataSet>
("{\"tab1\":[{\"col1\":\"val1\"}]}"); // OK
DataTable dt = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>
("{\"col1\":\"val1\"}"); // System.OutOfMemoryException
why?
thank you
IMO it's a bug, But if you want to deserialize DataSet or DataTable this might help you.
The JSON in your second example represents a only a single row of data, not a DataTable. A DataTable is an ordered collection of DataRows, therefore it needs to have square brackets in the JSON. Try it like this instead:
string json = "[{\"col1\":\"val1\"}]";
DataTable dt = JsonConvert.DeserializeObject<DataTable>(json);
I am not sure why you are getting an OutOfMemoryException; I get a JsonSerializationException when I try it, as I would expect. Perhaps this was a bug that was fixed in the most recent version of Json.Net.
I have over come with this issue by creating JsonSerializer object. My code is as given below.
using (StreamReader r = new StreamReader(yourfilePath))
{
using (JsonReader reader = new JsonTextReader(r))
{
JsonSerializer serializer = new JsonSerializer();
T lstObjects = serializer.Deserialize<T>(reader);
}
}
Here yourfilePath :- is your file current full path
T :- is your object name, whether it could be Dataset, DataTable or any custom Object.

Devexpress - Re-bind Chart Control error Index was out of range

I has using dexexpress chartcontrol and bind the datasource in runtime.
chartControl1.DataSource = ds.Tables[0];
chartControl1.SeriesDataMember = "Task";
chartControl1.SeriesTemplate.ArgumentDataMember = "Resource";
chartControl1.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Percentage" });
chartControl1.SeriesTemplate.View = new StackedBarSeriesView();
The first time binding, it has work fine and can show the chart.
When I click a button to re-create the dataset with new row of data, it give me an error in
chartControl1.DataSource = ds.Tables[0];
I has set the dataset = new dataset before fill it again with new data.
Any one has idea what wrong. Please help.
You should be able to do something along the lines of the following:
this.chartControl1.BeginInit();
DataTable chartData = this.chartControl1.DataSource as DataTable;
DataRow row = new DataRow()
{
"col1",
"col2"
}
chartData.Rows.Add(row);
this.chartControl1.RefreshData();
this.chartControl1.EndInit();
I hope this is helpful.
I have found that DevExpress occasionally gets confused when I set the DataSource, especially to an existing object. To work round this, before I set the DataSource to anything, I always set it to null first. Since doing that, I have not had any problems.
Give that a try.

defaultview not returning all rows

I have a dataset. it has 32 rows.I am not applying any rowfilter to it.code is here
private void SetPageNumbers(DataSet dsQuestion)
{
DataView dv = dsQuestion.Tables[0].DefaultView;
}
It is showing only 4 rows in dv.
But I want all rows. What can be the reason behind it?
I also noticed that defaultview is showing Rowfilter as "SectionId=4".But I have defined this in another function.
Try this
private void SetPageNumbers(DataSet dsQuestion)
{
DataView dv = new DataView(dsQuestion.Tables[0]);
}
I done it by adding following line;
dtques.DefaultView.RowFilter = string.Empty;
Can anyone answer why Defaultview got its value in different function. Is it attached to dataset.

Eval versus DataField in ASP:Datagrid

I have this really random problem with is bugging me. It works at the end of the day but the problem took some time to figure out and was wondering why this happens so if anyone shed some light on the subject I would be really grateful. Here is the problem
I have the following two columns on my datagrid
<asp:boundcolumn
datafield="contentsection.Id"
headerstyle-cssclass="dgHead"
headertext="Section"
itemstyle-cssclass="dgItem" />
and
<asp:templatecolumn>
<itemtemplate><%#Eval("contentsection.Id") %></itemtemplate>
</asp:templatecolumn>
The first column gives me the error of:
A field or property with the name 'contentsection.Id' was not found on the selected data source
but the second on runs fine. Any ideas or theories as to why this is happening ?
The way that I call and bind my data is like so:
Page Load Code Behind
ContentList.DataSource = ContentBlockManager.GetList();
ContentList.DataBind();
The GetList() function it is overloaded and by default passed a 0
public static List<ContentBlockMini> GetList(int SectionId)
{
List<ContentBlockMini> myList = null;
SqlParameter[] parameters = { new SqlParameter("#ContentSectionId", SectionId) };
using (DataTableReader DataReader = DataAccess.GetDataTableReader("dbo.contentblock_get", parameters))
{
if (DataReader.HasRows)
{
myList = new List<ContentBlockMini>();
}
while (DataReader.Read())
{
myList.Add(FillMiniDataRecord(DataReader));
}
}
return myList;
}
and my filling of the data record. The ContentSection is another Object Which is a property of the ContentBlock object
private static ContentBlockMini FillMiniDataRecord(IDataRecord DataRecord)
{
ContentBlockMini contentblock = new ContentBlockMini();
contentblock.Id = DataRecord.GetInt32(DataRecord.GetOrdinal("Id"));
contentblock.Name = DataRecord.GetString(DataRecord.GetOrdinal("Name"));
contentblock.SEOKeywords = DataRecord.IsDBNull(DataRecord.GetOrdinal("SEOKeywords")) ? string.Empty : DataRecord.GetString(DataRecord.GetOrdinal("SEOKeywords"));
contentblock.SEODescription = DataRecord.IsDBNull(DataRecord.GetOrdinal("SEODescription")) ? string.Empty : DataRecord.GetString(DataRecord.GetOrdinal("SEODescription"));
if (DataRecord.GetInt32(DataRecord.GetOrdinal("ContentSectionId")) > 0)
{
ContentSection cs = new ContentSection();
cs.Id = DataRecord.GetInt32(DataRecord.GetOrdinal("ContentSectionId"));
cs.Name = DataRecord.IsDBNull(DataRecord.GetOrdinal("ContentSectionName")) ? string.Empty : DataRecord.GetString(DataRecord.GetOrdinal("ContentSectionName"));
contentblock.contentsection = cs;
}
return contentblock;
}
There you go that is pretty much the start to end.
Databinding can only access "top level" properties on an object. For example, if my object is
Company with simple properties CompanyId and Name and a child object for Address, databinding can only access CompanyId and Name, not Company.Address.City.
Eval can access child object properties if you import the namespace using an #Import page directive.
The "Bind" marker is a two-way bind and is made using the default namespace attached to the datasource of the object. This can't be overridden, and because of this tightly coupled connection the namespace should be left off as assumed (since it's dynamically pulled from the datasource).
Eval is a one-way binding that can be used with any namespace that will fulfill a proper evaluation. Dates, strings, method calls, etc. The namespace provided in your example just provides eval with a marker on where to get the data relevant.
The key here is the nature of the binding. Eval is a "fire and forget" sort of binding where as Bind is more rigid to facilitate the two-way.
The DataField property approach isn't complicated enough to support dot notation; it expects a direct reference in your underlying data source; it takes that field and checks the data source, which it can't find it by the exact string.
The second approach, eval, is more dynamic, but actually I wasn't sure if it supported dot notation... learn something everyday.
HTH.
It's necessary take a look to your DataSource, but you can do a simple test: convert the first column to a template column a try to Run.
In EntityFramework is possible Eval Properties using code like this: <%#Eval("Customer.Address.PostalCode") %>.
I dont know if it is your case, but if you provide more details about how to retrieve the DataSource we can help you.

Resources