I have a DataTable that I fill manually, ie,
newrow = dt.NewRow();
dt.Rows.Add(newrow);
and Im trying to benefit from the groupby features of LinqDataSource (as shown by Matt) by linking the LinqDataSource to the DataTable, but its just not happening.
Has anyone any experience with this?
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public class MyData
{
public MyData(){}
private DataTable dt = new DataTable("dt");
public DataTable MyTable
{
get { return dt; }
set { dt = value; }
}
}
protected void Page_Load(object sender, EventArgs e)
{
MyData mydata = new MyData();
mydata.MyTable.Columns.Add("column1");
DataRow dr = mydata.MyTable.NewRow();
dr[0] = "some data";
mydata.MyTable.Rows.Add(dr);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:LinqDataSource
ID="LinqDataSource1"
runat="server"
ContextTypeName="MyData"
TableName="MyTable">
</asp:LinqDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSource1">
</asp:GridView>
</form>
</body>
</html>
"dt" is not the name of the table try this
DataTable dt = new DataTable("dt");
and then try this would name the table as "dt".
No that just won't work at all. The ContextTypeName property needs to be that of a Context like a LinqtoSql DataContext and the TableName is the name of the database table that is mapped in your LinqToSql DBML file. Have you tried actually following the article you link to as it gives a pretty good overview of getting LinqToSQL going.
Related
I have been created simple shopify app to retrieve product details.
When i access code from gihub.
It run successfully and display product details in text box.
I need to simple change to show the product details in grid view.
Here is dafault.aspx:
existing code;
default.aspx:
<%# Page Language="C#" AutoEventWireup="true" codefile="Default.aspx.cs" Inherits="SampleWebApplication._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="APIOutput" TextMode="MultiLine" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
Here is my output screenshot http://s22.postimg.org/xj9zacxa9/untitled.jpg
I need to display product details in gridview,
default.cs:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopifyAPIAdapterLibrary;
using System.Configuration;
using System.Web.Services;
namespace SampleWebApplication
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ShopifyAuthorizationState state = HttpContext.Current.Session["Shopify.AuthState"] as ShopifyAuthorizationState;
ShopifyAPIClient client
= new ShopifyAPIClient(state);
APIOutput.Text = (string)client.Get("/admin/products.json");
}
}
}
but I just confused with codefile, Can anyone help me get the product details in gridview?
Any help would be highly appreciated.
Thanks in advance.
Try to add reference to json service from visual studio. Then you will have proper classes for your json data, which you can easily parse for yours TextBox.
Using JSON.net you can convert your JSON string to DataTable.
First you need to add JSON.net DLL (http://json.codeplex.com/) in your project. Then add its namespace in your Webpage.
Now DerializeDataTable will return datatable, you need to replace string json variable with your SHOPIfY json result;
public DataTable DerializeDataTable()
{
const string json = #"[{""Name"":""AAA"",""Age"":""22"",""Job"":""PPP""},"
+ #"{""Name"":""BBB"",""Age"":""25"",""Job"":""QQQ""},"
+ #"{""Name"":""CCC"",""Age"":""38"",""Job"":""RRR""}]";
var table = JsonConvert.DeserializeObject<datatable>(json);
return table;
}
Code to bind GridView:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
gvBind(); //Bind gridview
}
}
public void gvBind()
{
var myTable=DerializeDataTable()
gvproducts.DataSource = myTable;
gvproducts.DataBind();
}
Edited:
protected void Page_Load(object sender, EventArgs e)
{
ShopifyAuthorizationState state = HttpContext.Current.Session["Shopify.AuthState"] as ShopifyAuthorizationState;
ShopifyAPIClient client
= new ShopifyAPIClient(state);
var myJsonString = (string)client.Get("/admin/products.json");
var table = JsonConvert.DeserializeObject<datatable>(myJsonString );
gvproducts.DataSource = table ;
gvproducts.DataBind();
}
I have a problem here
I have year drop down list in my page, for that i am binding items from code behind,The maximum year i am adding from code behind is 2027.
But one user came up ,who wants to select year 2040, I am wondering whether I can manually add year 2040 from aspx page, so that no need to deploy my code.
Please help on this.
Many Thanks
It is not a very good way but you can do this using javascript. below is the code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function myFunction() {
var x = document.getElementById("DropDownList1");
var option = document.createElement("option");
option.text = "2040";
x.add(option);
}
</script>
</head>
<body onload="myFunction();">
<form id="form1" runat="server">
<div>
</div>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</form>
</body>
</html>
cs code just to show that drop down is some element of dropdown filled in server side code
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.DataSource = new int[] { 1, 2, 3 };
DropDownList1.DataBind();
}
I want to add the server name in my page head section dynamically like
<head>
<!-- DP2-WEB005 -->
</head>
can anyone please let me know how can I add this <!-- DP2-WEB005 --> tag in head section.
server name I will handle it but I don't know how add that commented tag dynamically.
HtmlGenericControl newControl = new HtmlGenericControl("someTag");
newControl.Attributes["someAttr"] = "some value";
Page.Header.Controls.Add(newControl);
I hope this helps ...
(the reference)
UPDATE:
This is that you want :
string serverName = "QWERTY123";
this.Page.Header.Controls.Add(new LiteralControl("<!-- " + serverName + "-->"));
And here is the output :
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title><!-- QWERTY123--></head>
<head runat="server">
<%= serverName %>
</head>
In code behind
public string serverName{get;set;}
protected void Page_Load(object o, EventArgs e)
{
serverName="assign";
}
Try like this
Aspx:
<html>
<head runat="server">
<%= Content %>
</head>
<body>
//Code here
</body>
</html>
Code Behind:
In Code behind write the following code in PageLoad()
public string Content{get;set;}
protected void Page_Load(object sender, EventArgs e)
{
String Content = "Content here";
}
If you want to add css or java-script in your page section you can use the following
var myHtmlLink = new HtmlLink { Href = #"filepath" };
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");
Page.Header.Controls.AddAt("0", myHtmlLink);
My gridview source code is following
<asp:GridView ID="g1" runat="server"></asp:GridView>
and my code behind is,
bindGridView(ds.Tables[0], g1, lblErrorActivityGrid, lblActivityGridCount);
private void bindGridView(DataTable dt, GridView grd, Label lblError, Label GrdRowCount)
{
grd.DataSource = dt;
grd.DataBind();
}
my question is why my gridview is not displaying data?
I am using dataset and manually adding data in my dataset and then returning table at 0th table.
Please help. Thanks in advance
In markup set AutogenerateColumns="true" :
<asp:GridView ID="g1" AutoGenerateColumns= "true" runat="server"></asp:GridView>
Here's how I have tested your code:
In my markup I have the gridview.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="g1" AutoGenerateColumns= "true" runat="server"></asp:GridView>
<br />
<asp:Label ID="lblErrorActivityGrid" runat="server" Text="Error Activity Grid"></asp:Label>
<br />
<asp:Label ID="lblActivityGridCount" runat="server" Text="Activity Grid Count"></asp:Label>
</div>
</form>
</body>
</html>
In my code, in page load I am adding a DataTable to a DataSet and calling bindGridView method:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("Column1", typeof(string));
dt.Columns.Add("Column2", typeof(string));
ds.Tables.Add(dt);
for (int i=0; i<5; i++)
{
DataRow nw = ds.Tables[0].NewRow();
nw[0] = (i + 1).ToString();
nw["Column1"] = (i + 1).ToString();
nw["Column2"] = String.Format("Item {0}", i);
ds.Tables[0].Rows.Add(nw);
}
bindGridView(ds.Tables[0], g1, lblErrorActivityGrid, lblActivityGridCount);
}
}
private void bindGridView(DataTable dt, GridView grd, Label lblError, Label GrdRowCount)
{
grd.DataSource = dt;
grd.DataBind();
}
And here's the grid shows in my browser:
Try to Set
grd.DataSource = dt.DefaultView;
you must set the View to the Gridview from the DataTable..
Make sure the Table has Data.
I have problem in implementing google maps api in asp.net. Here is my aspx code:
<%# Page Title="" Language="C#" AutoEventWireup="true" CodeFile="visual_loc.aspx.cs" Inherits="visual_loc" %>
<%--A sample project by Ghaffar khan--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Your Data on Google Map </title>
<%--Google API reference--%>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false
&key=asdfg" type="text/javascript">
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server">
<%--Place holder to fill with javascript by server side code--%>
<asp:Literal ID="js" runat="server"></asp:Literal>
<%--Place for google to show your MAP--%>
<div ID="map_canvas" style="width: 100%; height: 728px; margin-bottom: 2px;">
</div>
<br />
</asp:Panel>
<br />
</form>
</body>
</html>
And my code behind: getLocation() is the method which gets the longitude and latitude from my database. createDataTable() is method which create DataTable from those locations.
protected void Page_Load(object sender, EventArgs e)
{
string user_id;
user_id = Request.Cookies["cookie"]["Login"];
getLocation(user_id);
BuildScript( createDataTable());
}
private void BuildScript(DataTable tbl)
{
String Locations = "";
foreach (DataRow r in tbl.Rows)
{
// bypass empty rows
if (r["Latitude"].ToString().Trim().Length == 0)
continue;
string Latitude = r["Latitude"].ToString();
string Longitude = r["Longitude"].ToString();
// create a line of JavaScript for marker on map for this record
Locations += Environment.NewLine + " map.addOverlay(new GMarker(new GLatLng(" + Latitude + "," + Longitude + ")));";
}
// construct the final script
js.Text = #"<script type='text/javascript'>
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById('map_canvas'));
map.setCenter(new GLatLng(52.259, 21.012), 2);
" + Locations + #"
map.setUIToDefault();
}
}
</script> ";
}
whole operation result in displaying empty site with no content. What am i doing wrong?
Problem was that i did wrong directories to my markers, so that cannot be seen on Map. Once i did enter valid directories everything works.