List sparql result into asp.net component - asp.net

I want to list sparql query result into textbox(multi line) or Grid view or list
But the code shown below returns only one result!
Please any help?
IGraph g = new Graph();
g.LoadFromFile("example.owl");
try
{
SparqlQueryParser par = new SparqlQueryParser();
SparqlQuery q = par.ParseFromString(#"PREFIX uni:<http://www.semanticweb.org/salim/ontologies/2018/10/university-ontology-2#>SELECT ?P_Name (COUNT(?P_Name) AS ?Material_Num)
WHERE
{
?P uni:Have ?Material;
uni:P_Name ?P_Name.
}
GROUP BY ?P_Name");
object results = g.ExecuteQuery(q);
if (results is SparqlResultSet)
{
SparqlResultSet rset = (SparqlResultSet)results;
foreach (SparqlResult r in rset)
{
TextBox1.Text = r.ToString();
//or
GridView1.DataSource = r.ToString();
GridView1.DataBind();
}
}
}

Related

How to perform Rollback in asp.net when using Stored Procedure

I want to insert data into 12 different tables on a single click of a button. For this I am using single stored procedure. But my problem is when I am doing this and if there is any exception occurs my data is getting inserted partially i.e values is getting inserted in some tables and some remains empty and due to this problem occurs since all are related to one another. So wanted to know is there any way to perform Rollback so that if any exception occurs entire query is rolled back and data is not inserted in any of the table.
This is the code I am currently using for inserting values.
public int Sp_InsertUpdateDelete(string s, SqlParameter[] spa)
{
SqlConnection sc = new SqlConnection(cs);
sc.Open();
SqlCommand scm = new SqlCommand(s, sc);
scm.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter sql in spa)
{
scm.Parameters.Add(sql);
}
int k = scm.ExecuteNonQuery();
sc.Close();
return k;
}
protected void btnHostingSubmit_Click(object sender, EventArgs e)
{
string select = "select * from tbl_Hosting where Customer_Id='" + ddlCustomerName.SelectedValue + "'";
DataSet s = gs.select(select);
if (s.Tables[0].Rows.Count > 0)
{
Response.Write("<script>alert('Customer Already Exist');</script>");
}
else
{
if (ddlHosting.SelectedValue == "Yes")
{
SqlParameter[] spa = new SqlParameter[29];
spa[0] = new SqlParameter("#Customer_Id", Convert.ToInt16(ddlCustomerName.SelectedValue));
spa[1] = new SqlParameter("#Type", 2);
//Hosting
if (txtHostingSDate.Text == "" || txtHostingSDate.Text == null)
{
spa[2] = new SqlParameter("#Hosting_start_date", null);
}
else
{
spa[2] = new SqlParameter("#Hosting_start_date", Convert.ToDateTime(txtHostingSDate.Text));
}
if (txtHosingEDate.Text == "" || txtHosingEDate.Text == null)
{
spa[3] = new SqlParameter("#Hosting_end_date", null);
}
else
{
spa[3] = new SqlParameter("#Hosting_end_date", Convert.ToDateTime(txtHosingEDate.Text));
}
spa[4] = new SqlParameter("#Hosting_provider", ddlHostingPro.SelectedItem.ToString());
spa[5] = new SqlParameter("#Hosting_type", ddlHostingType.SelectedItem.ToString());
spa[6] = new SqlParameter("#Hosting_server", ddlHostingServer.SelectedItem.ToString());
spa[7] = new SqlParameter("#Hosting_total_id", Convert.ToInt16(txtHostingId.Text));
spa[8] = new SqlParameter("#Hosting_mail_tracking", ddlHostingMailTracking.SelectedItem.ToString());
spa[9] = new SqlParameter("#Hosting_mail_tracking_users", Convert.ToInt16(txtHostingMtUser.Text));
spa[10] = new SqlParameter("#Hosting_dns", ddlHostingDns.SelectedItem.ToString());
spa[11] = new SqlParameter("#Hosting_mail", ddlHostingMail.SelectedItem.ToString());
spa[12] = new SqlParameter("#Hosting_web", ddlHostingWeb.SelectedItem.ToString());
spa[13] = new SqlParameter("#Hosting_manage_dns", ddlHostingMngDns.SelectedItem.ToString());
if (ddlHostingDns.SelectedValue == "No" && (ddlHostingMail.SelectedValue == "Yes" || ddlHostingWeb.SelectedValue == "Yes"))
{
spa[14] = new SqlParameter("#Hosting_ns1", txtNS1.Text);
spa[15] = new SqlParameter("#Hosting_ns2", txtNS2.Text);
}
else
{
spa[14] = new SqlParameter("#Hosting_ns1", ddlHostingNS1.SelectedItem.ToString());
spa[15] = new SqlParameter("#Hosting_ns2", ddlHostingNS2.SelectedItem.ToString());
}
spa[16] = new SqlParameter("#Hosting_rec_ip", txtHostingARecordIp.Text);
spa[17] = new SqlParameter("#Hosting_mx_rec1", txtMXRecord1.Text);
spa[18] = new SqlParameter("#Hosting_mx_rec2", txtMXRecord2.Text);
spa[19] = new SqlParameter("#Hosting_mx_ip1", txtHostingMxIp1.Text);
spa[20] = new SqlParameter("#Hosting_space", ddlHostingSpace.SelectedItem.ToString());
spa[21] = new SqlParameter("#Hosting_mx_ip2", txtHostingMxIp2.Text);
spa[22] = new SqlParameter("#Hosting_data_transfer", ddlhostingDataTrans.SelectedItem.ToString());
spa[23] = new SqlParameter("#Hosting_manage_dns_amt", txtHostingMangDnsAmt0.Text);
spa[24] = new SqlParameter("#Hosting_amt", txtHostingAmt0.Text);
spa[25] = new SqlParameter("#Hosting_c_ns1", txtHostingNS1.Text);
spa[26] = new SqlParameter("#Hosting_c_ns2", txtHostingNS2.Text);
spa[27] = new SqlParameter("#Hosting_c_ns3", txtHostingNS3.Text);
spa[28] = new SqlParameter("#Hosting_c_ns4", txtHostingNS4.Text);
int k = gs.Sp_InsertUpdateDelete("Sp_Hosting", spa);
if (k > 0)
{
Response.Write("<script>alert('Hosting Added Success');</script>");
}
Clear();
}
using(SqlConnection conn = new SqlConnection())
{
try
{
conn.Open();
SqlTransaction tran = conn.BeginTransaction("Transaction1");
Cmd = new SqlCommand(sQuery, Conn);
Cmd.Transaction = tran;
//Your Code
tran.Commit(); //both are successful
}
catch(Exception ex)
{
//if error occurred, reverse all actions. By this, your data consistent and correct
tran.Rollback();
}
}
https://msdn.microsoft.com/en-us/library/a90c30fy.aspx
You need to modify your Stored procedure and use Transactions for this feature.
Something like this:
DECLARE #TranName VARCHAR(20);
SELECT #TranName = 'MyTransaction';
BEGIN TRANSACTION #TranName;
USE AdventureWorks2012;
DELETE FROM AdventureWorks2012.HumanResources.JobCandidate
WHERE JobCandidateID = 13;
COMMIT TRANSACTION #TranName;
GO
MSDN Reference

how can i select data from datatable

My datatable contain column "Date" that have data like this in the following
'2013-01-01',
'2013-05-01',
'2014-01-01',
'2014-12-25',
'2014-12-26
But i want to get data something like 'select distinct year(Date)', and bind selected data into dropdownlist
however, syntax error message popup: "Syntax error: Missing operand after 'year' operator."
can I get any support? THANKS!!!!
private DataTable BuildDT(string [] date)
{
DataTable dt = new DataTable("test");
dt.Columns.Add("RowID", typeof(Int16));
dt.Columns.Add("Date", typeof(DateTime));
for (int i = 0; i < date.Length; i++)
{
dt.Rows.Add(i+1,date[i]); // {1,2013-01-01}
}
return dt;
}
private void ddlGetData2(DataTable table)
{
table.DefaultView.RowFilter = "distinct year(Date)";
DropDownList1.DataSource = table;
DropDownList1.DataBind();
}
You can do this with LINQ...
var years = table.AsEnumerable()
.Select(r => r.Field<DateTime>("Date").Year)
.Distinct()
.OrderBy(y => y)
.ToList();
You can use the .ToTable() on the associated DataView instance.
var dv = dt.DefaultView;
var distinctDates = dv.ToTable(true, "Date");

Editable gridview based on list

Is it possible to create a gridview based on a list? I have the following list:
ID = 1
Name = John
Zip = 33141
ID = 2
Name = Tim
Zip = 33139
I want to be able to create an editable gridview with this list
When i bind it to the grid view, it seems to put everyting in one column, and i can't figure out how to get it to seperate it into different columns
Here is my code for setting the DataSource of the GridView:
DataTable table = ConvertListToDataTable(personList);
GridView1.DataSource = table;
GridView1.DataBind();
static DataTable ConvertListToDataTable(List<string> list)
{
// New table.
DataTable table = new DataTable();
// Get max columns.
int columns = 7;
// Add columns.
for (int i = 0; i < columns; i++)
{
table.Columns.Add();
}
// Add rows.
foreach (var rd in list)
{
table.Rows.Add(rd);
}
return table;
}
Here is an example:
private class Person
{
int m_iID;
string m_sName;
string m_sZip;
public int ID { get { return m_iID; } }
public string Name { get { return m_sName; } }
public string Zip { get { return m_sZip; } }
public Person(int iID, string sName, string sZip)
{
m_iID = iID;
m_sName = sName;
m_sZip = sZip;
}
}
private List<Person> m_People;
private void ConvertListToDataTable(List<Person> People)
{
DataTable table = new DataTable();
DataColumn col1 = new DataColumn("ID");
DataColumn col2 = new DataColumn("Name");
DataColumn col3 = new DataColumn("Zip");
col1.DataType = System.Type.GetType("System.String");
col2.DataType = System.Type.GetType("System.String");
col3.DataType = System.Type.GetType("System.String");
table.Columns.Add(col1);
table.Columns.Add(col2);
table.Columns.Add(col3);
foreach (Person person in People)
{
DataRow row = table.NewRow();
row[col1] = person.ID;
row[col2] = person.Name;
row[col3] = person.Zip;
table.Rows.Add(row);
}
GridView1.DataSource = table;
GridView1.DataBind();
}

specific distance between columns of drop down list

i like to have 2 columns in my drop down list with specific distance between 2 columns, so i have added below code but columns are not align with my code.
var query = from p in _DataContext.tblDocuments
orderby p.DocumentNo
select new
{
Doctitle = p.DocumentNo+' '+' '+"|"+' '+p.TITLE,
DocId = p.DocId
};
ddlProjectDocument.DataSource = query;
ddlProjectDocument.DataValueField = "DocId";
ddlProjectDocument.DataTextField = "Doctitle";
ddlProjectDocument.DataBind();
please help how i can have 2 columns with normal view and good align.
The alignment will depend on the length of the value in the field of your table. One way to do it would be to get the size of the largest entry in your table and make sure that all entries are that wide. Here is something you can try.
<asp:DropDownList ID="ddlStack" runat="server" OnLoad="ddlStack_Load" />
and in the source code for ddl load event:
protected void ddlStack_Load(object sender, EventArgs e)
{
var all = from o in _DataContext.tblDocuments
orderby o.DocumentNo
select o;
int maxs = 0;
foreach (tblDocuments v in all)
{
if (v.DocumentNo.Length > maxs)
maxs = v.DocumentNo.Length;
}
foreach (tblDocuments vv in all)
{
string doctitle = vv.DocumentNo;
for (int i = vv.DocumentNo.Length; i < maxs + 2; i++)
{
doctitle += '_';
}
doctitle += " | ";
doctitle += vv.DocID;
ddlStack.Items.Add(new ListItem(doctitle, vv.vendorID.ToString()));
}
}
You can try either from the below two methods:
1). select new
{
Doctitle = p.DocumentNo+" "+"|"+" "+p.TITLE,
DocId = p.DocId
};
2).
select new
{
Doctitle =Concat(p.DocumentNo,p.TITLE),
DocId = p.DocId
};

How to differentiate between two similar fields in Linq Join tables

How to differentiate between two select new fields e.g. Description
c.Description and lt.Description
DataTable lDt = new DataTable();
try
{
lDt.Columns.Add(new DataColumn("AreaTypeID", typeof(Int32)));
lDt.Columns.Add(new DataColumn("CategoryRef", typeof(Int32)));
lDt.Columns.Add(new DataColumn("Description", typeof(String)));
lDt.Columns.Add(new DataColumn("CatDescription", typeof(String)));
EzEagleDBDataContext lDc = new EzEagleDBDataContext();
var lAreaType = (from lt in lDc.tbl_AreaTypes
join c in lDc.tbl_AreaCategories on lt.CategoryRef equals c.CategoryID
where lt.AreaTypeID== pTypeId
select new { lt.AreaTypeID, lt.Description, lt.CategoryRef, c.Description }).ToArray();
for (int j = 0; j< lAreaType.Count; j++)
{
DataRow dr = lDt.NewRow();
dr["AreaTypeID"] = lAreaType[j].LandmarkTypeID;
dr["CategoryRef"] = lAreaType[j].CategoryRef;
dr["Description"] = lAreaType[j].Description;
dr["CatDescription"] = lAreaType[j].;
lDt.Rows.Add(dr);
}
}
catch (Exception ex)
{
}
You can give them an explicit name when selecting:
select new { lt.AreaTypeID, LtDescr = lt.Description, lt.CategoryRef, CDescr = c.Description }
And then:
dr["Description"] = lAreaType[j].LtDescr;
dr["CatDescription"] = lAreaType[j].CDescr;
Change:
select new { lt.AreaTypeID, lt.Description, lt.CategoryRef, c.Description }
To:
select new { AreaTypeID = lt.AreaTypeID,
LtDescription = lt.Description,
CategoryRef = lt.CategoryRef,
CatDescription = c.Description }
That will give each property in the anonymous type a different explicit name rather than simply relying on the existing name. You can then access them later using:
dr["Description"] = lAreaType[j].LtDescription;
dr["CatDescription"] = lAreaType[j].CatDescription;

Resources