I want to send data from c#(aspx) to android application.
My c#(aspx)page have code which is called from android application, that retrieve values from sql database and put it into an XML file using DataSet and return back xml file to android application.
The code for retrieving data from database and storing in xml is as below:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.IO;
class ExecuteXmlReader
{
public static void Main()
{
String sConnection = "server=.\\SQLExpress;Integrated Security=SSPI;database=employee";
SqlConnection mySqlConnection = new SqlConnection(sConnection);
mySqlConnection.Open();
// Get the same data through the provider.
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("SELECT FirstName,LastName,Title,City from employees", sConnection);
DataSet myDataSet2 = new DataSet();myDataSet2.DataSetName = "Region";
mySqlDataAdapter.Fill(myDataSet2);
// Write data to files: data1.xml and data2.xml for comparison.
myDataSet2.WriteXml("c:/temp/employees.xml");
mySqlConnection.Close();
}
}
now how do I return this xml file to android application. In android I am using DOM XML parser to extract data from file.
Please help me in this.. any tips will be accepted.
use this it has sample wcf webservice with consumption in android-http://fszlin.dymetis.com/post/2010/05/10/Comsuming-WCF-Services-With-Android.aspx.hope this helps you
Related
I'm trying to save the Outlook attachment file email and save in sql. I tried this following code and it only saved a numerical values
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace RetrieveEmail
{
public class Program
{
static void Main(string[] args)
{
Outlook.Application oLk = new Outlook.Application();
Outlook._NameSpace olNS = oLk.GetNamespace("MAPI");
olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
Outlook.Items oItems = oFolderIn.Items;
foreach (object item in oFolderIn.Items)
{
if (item is Outlook.MailItem oMailItem)
{
SqlConnection con = new SqlConnection(#"Data Source=\SQLEXPRESS; initial catalog=EmailReply;Integrated Security=True");
SqlCommand cmd = new SqlCommand("INSERT INTO Emails (SenderName, Subject, Body, Attachment) VALUES (#SenderName, #Subject, #Body, #Attachment)", con);
cmd.Parameters.AddWithValue("#SenderName", oMailItem.SenderName);
cmd.Parameters.AddWithValue("#Subject", oMailItem.Subject);
cmd.Parameters.AddWithValue("#Body", oMailItem.Body);
cmd.Parameters.AddWithValue("#Attachment", oMailItem.EnableSharedAttachments); // I tried this code to extract Outlook attachment file email
con.Open();
int k = cmd.ExecuteNonQuery();
if (k != 0)
{
Console.WriteLine("Record Inserted Succesfully into the Database");
}
con.Close();
}
}
}
}
}
This is what saved in my sql, it didn't save the attachment I'm trying to save the attachment file with extension of (.docx .pdf .excel .pptx)
You need to save the attached file on the disk first. The Outlook object model doesn't provide any property or method for getting the attachment content on the fly at runtime. Use the Attachment.SaveAsFile method which saves the attachment to the specified path.
Extended MAPI allows reading the attachment bytes on the fly by using the PR_ATTACH_DATA_BIN property holds the attachment when the value of the PR_ATTACH_METHOD property is ATTACH_BY_VALUE, which is the usual attachment method and the only one required to be supported. You may consider using any third-party wrapper around Extended MAPI such as Redemption for getting the binary data of attached files.
I created a console application which accepts arguments. After building the program i will run it through cmd in which the user will input like this "filemgr.exe create [filename] [contents]" . My code is below. I want to enter "my text here" content, but when i check the output, only the FIRST word is displayed which is the "my", how to include the rest of the strings?(which is the text here)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace filemgr
{
class Program
{
static void Main(string[] args)
{
if (args[0]=="create")
{
using (StreamWriter file = new StreamWriter(args[1]))
{
file.Write(args[2]);
}
}
}
}
}
You need to recombine the args array into a string (although this method will likely lose quotes):
file.Write(string.Join(" ", args.Skip(2)));
Alternatively you can quote the string in the commandline:
program.exe create test.txt "hello this is a message"
Also, if you want to append to the file through sequential calls you need to open the stream like so:
using (StreamWriter file = new StreamWriter(args[1], true))
This signifies that you want append mode, and not create/replace.
Pass content args in double quotes
filemgr.exe create [filename] "[contents]"
filemgr.exe create C:\test.txt "my file content"
I would suggest to use quotes even in file path as well. When you use args, its always a chance to make mistake while accessing arguments.
I have this assembly that basically creates an empty excel file using EPPLUS library. The file is created directly to desk, but i want first to write it in a memoerystream and then save the memoerystream to desk. I have this so far.
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationXLSX
{
class Program
{
static void Main(string[] args)
{
Createxlsx("XLSX");
}
public static void Createxlsx(string filename)
{
FileInfo newFile = new FileInfo("C:\\ConsoleApplicationXLSX\\" + filename + ".xlsx");
MemoryStream ms = new MemoryStream();
//create a package
using (ExcelPackage package = new ExcelPackage(ms))
{
package.Workbook.Worksheets.Add("worksheet");
package.Save();
}
}
}
}
Try
package.SaveAs(ms)
That should fill your stream with the excelfile-content.
I have called to a web service which returns plain text of xml web service. What I want is to get that plain text (string) into data table.how can I do that.
this is my code.
WebClient client = new WebClient();
string url = string.Format("https://www.someurl/products.ashx");
string response = client.DownloadString(url);
this code retruns the string(plain text) of web service.
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
namespace ConsoleApplication56
{
class Program
{
static void Main(string[] args)
{
string response = "";
XmlReader reader = XmlReader.Create(response);
DataSet ds = new DataSet();
ds.ReadXml(reader);
}
}
}
I was trying to extract the data from pdf file using iTextSharp, but i go two errors. Actually I want to extract the data from pdf file and store it into database.
Here is my code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.util.collections;
using System.Linq;
public partial class frm_CreatePDF : System.Web.UI.Page
{
public string P_InputStream3 = "~/My Documents/List Of Holidays 2012";
protected void Page_Load(object sender, EventArgs e)
{
ExtractText();
}
private string ExtractText()
{
PdfReader reader = new PdfReader(Server.MapPath(P_InputStream3));
string txt = PdfTextExtractor.GetTextFromPage(reader, 2, new LocationTextExtractionStrategy());
return txt;
}
}
And the error is:
The name 'PdfTextExtractor' does not exist in the current context
The type or namespace name 'LocationTextExtractionStrategy
Make sure you have brought the namespace in which those two classes are defined into scope by adding the using directive:
using iTextSharp.text.pdf.parser;
I guess you need to use another namespace where LocationTextExtractionStrategy is defined
First of all you need to add a reference to the ItextSharp dll, after wich you can add a using statement to acces the namespace that contains that static class
http://msdn.microsoft.com/en-us/library/wkze6zky%28v=vs.100%29.aspx
This is a late answer, but I've found what your issue is:
You are missing the iTextSharp.text.pdf.parser using statement. Add this below your using iTextSharp.text.pdf; code:
using iTextSharp.text.pdf.parser;
That should be able to find your LocationTextExtractionStrategy pretty well.
You can also just add iTextSharp.text.pdf.parser directly behind your LocationTextExtractionStrategy if you want.
string txt = PdfTextExtractor.GetTextFromPage(reader, 2, new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy());
But I would recommend the former. It's cleaner and easier to read.