Click here to Skip to main content
15,889,852 members
Home / Discussions / C#
   

C#

 
GeneralRe: Parse 17,000 text files and insert into database Pin
Gerry Schmitz15-Nov-16 8:19
mveGerry Schmitz15-Nov-16 8:19 
GeneralRe: Parse 17,000 text files and insert into database Pin
peterkmx15-Nov-16 8:50
professionalpeterkmx15-Nov-16 8:50 
AnswerRe: Parse 17,000 text files and insert into database Pin
Philippe Mori15-Nov-16 8:11
Philippe Mori15-Nov-16 8:11 
AnswerRe: Parse 17,000 text files and insert into database Pin
Nathan Minier15-Nov-16 8:33
professionalNathan Minier15-Nov-16 8:33 
AnswerRe: Parse 17,000 text files and insert into database Pin
Mycroft Holmes15-Nov-16 11:54
professionalMycroft Holmes15-Nov-16 11:54 
AnswerRe: Parse 17,000 text files and insert into database Pin
Bernhard Hiller15-Nov-16 21:33
Bernhard Hiller15-Nov-16 21:33 
AnswerRe: Parse 17,000 text files and insert into database Pin
Jimmanuel23-Nov-16 10:48
Jimmanuel23-Nov-16 10:48 
QuestionDevelop Web Service REST Pin
Member 1283622014-Nov-16 3:37
Member 1283622014-Nov-16 3:37 
I want to develop a Web Service restful CRUD between a data base PostgreSQL and a web application in asp. I look many examples and tutorials but I've not find solution. I'am here :
I have a service "Service191" that I can call by Mozilla or by WCF Test Client :
C#
public class Service191 : IService191
{
    public string data;
    public static NpgsqlConnection conn;

    /*
     * Connection à la base de donnée
     */
    public void connection()
    {
        try
        {
                Mails mail = new Mails();
                string strConnString = "Server=194.206.X.XXX; Port=5432; Database=XXXXX; User Id=XXXX; Password=XXXXX";
                DAL.DAL dal = new DAL.DAL(strConnString);

                //TestSelectCommand(mail, dal);
                //TestXMLSerialization();


                GenerateGetRequest();
                //GeneratePostRequest();


        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        } 
    }

For test this I call the function GenerateGetRequest() :

C#
private static void GenerateGetRequest()
    {
        string url = "http://localhost:49761/Service191.svc/mails?id=14";
        HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
        GETRequest.Method = WebRequestMethods.Http.Get;

       HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
        Stream GetResponseStream = GETResponse.GetResponseStream();
        StreamReader sr = new StreamReader(GetResponseStream);

        MessageBox.Show(sr.ReadToEnd());
    }


Url is what my web application send later. "Mails" is the table where the web service will do the request. I've got too a Handler :

C#
public class Handler : IHttpHandler
{
    private DAL.DAL dal;
    private string connString;
    private Mails mail;
    private ErrorHandler.ErrorHandler errHandler;

    #region HANDLER

    public bool IsReusable
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public void ProcessRequest(HttpContext context)
    {
        try
        {
            string url = Convert.ToString(context.Request.Url);
            connString = "Server = 194.206.X.XXX; Port = 5432; Database = XXXX; User Id = XXXX; Password = XXXXX";
            dal = new DAL.DAL(connString);
            errHandler = new ErrorHandler.ErrorHandler();

            switch (context.Request.HttpMethod)
            {
                case "GET":
                    READ(context);
                    break;
                case "POST":
                    CREATE(context);
                    break;
                case "PUT":
                    UPDATE(context);
                    break;
                case "DELETE":
                    DELETE(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            errHandler.ErrorMessage = ex.Message.ToString();
            context.Response.Write(errHandler.ErrorMessage);
            //MessageBox.Show(ex.ToString());
        }
    }

    #endregion

    #region CRUD

    private void READ(HttpContext context)
    {
        try
        {
            int id = Convert.ToInt16(context.Request["id"]);
            mail = dal.GetMail(id);
            if (mail == null)
                context.Response.Write(id + "No mail found");

            string serialized = Serialize(mail);
            context.Response.ContentType = "text/xml";
            WriteResponse(serialized);
            MessageBox.Show("mail READ");
        }
        catch (Exception ex)
        {
            errHandler.ErrorMessage = dal.GetException();
            errHandler.ErrorMessage = ex.Message.ToString();
            //MessageBox.Show(ex.ToString());
        }
    }

    private void CREATE(HttpContext context)
    {
        try
        {
            byte[] PostData = context.Request.BinaryRead(context.Request.ContentLength);
            string str = Encoding.UTF8.GetString(PostData);
            Mails mail = Deserialize(PostData);
            dal.AddMail(mail);
            MessageBox.Show("mail CREATE");
        }
        catch (Exception ex)
        {
            errHandler.ErrorMessage = dal.GetException();
            errHandler.ErrorMessage = ex.Message.ToString();
            //MessageBox.Show(ex.ToString());
        }

    }

    private void UPDATE(HttpContext context)
    {
        try
        {
            byte[] PUTRequestByte = context.Request.BinaryRead(context.Request.ContentLength);
            context.Response.Write(PUTRequestByte);

            Mails mail = Deserialize(PUTRequestByte);
            dal.UpdateMail(mail);
            MessageBox.Show("mail UPDATE");
        }
        catch (Exception ex)
        {
            errHandler.ErrorMessage = dal.GetException();
            errHandler.ErrorMessage = ex.Message.ToString();
            //MessageBox.Show(ex.ToString());
        }
    }

    private void DELETE(HttpContext context)
    {
        try
        {
            int id = Convert.ToInt16(context.Request["id"]);
            dal.DeleteMail(id);
            MessageBox.Show("mail DELETE");
        }
        catch (Exception ex)
        {
            errHandler.ErrorMessage = dal.GetException();
            errHandler.ErrorMessage = ex.Message.ToString();
        }
    }

    #endregion

    private Mails Deserialize (byte[] xmlByteData)
    {
        try
        {
            XmlSerializer ds = new XmlSerializer(typeof(Mails));
            MemoryStream memoryStream = new MemoryStream(xmlByteData);
            Mails mail = new Mails();
            mail = (Mails)ds.Deserialize(memoryStream);
            return mail;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            throw;
        }
    }

    private static void WriteResponse(string strMessage)
    {
        HttpContext.Current.Response.Write(strMessage);
    }

    private String Serialize(Mails mail)
    {
        try
        {
            String XmlizedString = null;
            XmlSerializer xs = new XmlSerializer(typeof(Mails));
            MemoryStream memoryStream = new MemoryStream();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            xs.Serialize(xmlTextWriter, mail);
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
            return XmlizedString;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
            throw;
        }
    }

    private String UTF8ByteArrayToString(Byte[] characters)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        String constructedString = encoding.GetString(characters);
        return (constructedString);
    }
}


But I don't understand why my handler is never call. So I have always a 400 error.

I've got too a DAL class who permiss the connection et request to the database :

C#
public class DAL
{
    private NpgsqlConnection conn;
    private NpgsqlCommand command;
    private static string connString;
    private static List<Mails> mailList;
    private ErrorHandler.ErrorHandler err;

    public DAL(string _connString)
    {
        err = new ErrorHandler.ErrorHandler();
        connString = _connString;
    }

    public void AddMail (Mails mail)
    {
        try
        {
            using (conn)
            {
                string npgsqlInsertString = "INSERT INTO mails (id_entete, emmetteur, destinataires, objet, contenu, date_envoi, heure_envoi) VALUES (@id_entete, @emmetteur, @destinataires, @objet, @contenu, @date_envoi, @heure_envoi)";

                conn = new NpgsqlConnection(connString);

                command = new NpgsqlCommand();
                command.Connection = conn;
                command.Connection.Open();
                command.CommandText = npgsqlInsertString;

                NpgsqlParameter idParam = new NpgsqlParameter("@id_entete", mail.Id_entete);
                NpgsqlParameter emmParam = new NpgsqlParameter("@id_entete", mail.Emmetteur);
                NpgsqlParameter destParam = new NpgsqlParameter("@id_entete", mail.Destinataires);
                NpgsqlParameter objParam = new NpgsqlParameter("@id_entete", mail.Objet);
                NpgsqlParameter contParam = new NpgsqlParameter("@id_entete", mail.Contenu);
                NpgsqlParameter dateParam = new NpgsqlParameter("@id_entete", mail.Date_envoi);
                NpgsqlParameter heureParam = new NpgsqlParameter("@id_entete", mail.Heure_envoi);

                command.Parameters.AddRange(new NpgsqlParameter[] { idParam, emmParam, destParam, objParam, contParam, dateParam, heureParam });
                command.ExecuteNonQuery();
                command.Connection.Close();

            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    public void UpdateMail (Mails mail)
    {
        try
        {
            using (conn)
            {
                string npgsqlUpdateString = "UPDATE mails SET id_entete=@id_entete, emmetteur=@emmetteur, destinataires=@destinataires, objet=@objet, contenu=@contenu, date_envoi=@date_envoi, heure_envoi=@heure_envoi WHERE id=@id";

                conn = new NpgsqlConnection(connString);

                command = new NpgsqlCommand();
                command.Connection = conn;
                command.Connection.Open();
                command.CommandText = npgsqlUpdateString;

                NpgsqlParameter idParam = new NpgsqlParameter("@id_entete", mail.Id_entete);
                NpgsqlParameter emmParam = new NpgsqlParameter("@id_entete", mail.Emmetteur);
                NpgsqlParameter destParam = new NpgsqlParameter("@id_entete", mail.Destinataires);
                NpgsqlParameter objParam = new NpgsqlParameter("@id_entete", mail.Objet);
                NpgsqlParameter contParam = new NpgsqlParameter("@id_entete", mail.Contenu);
                NpgsqlParameter dateParam = new NpgsqlParameter("@id_entete", mail.Date_envoi);
                NpgsqlParameter heureParam = new NpgsqlParameter("@id_entete", mail.Heure_envoi);

                command.Parameters.AddRange(new NpgsqlParameter[] { idParam, emmParam, destParam, objParam, contParam, dateParam, heureParam });
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }
        catch (Exception ex)
        {
            err.ErrorMessage = ex.Message.ToString();
            throw;
        }
    }

    public void DeleteMail (int id)
    {
        try
        {
            using (conn)
            {
                string npgsqlDeleteString = "DELETE FROM mails WHERE id=@id";

                conn = new NpgsqlConnection(connString);

                command = new NpgsqlCommand();
                command.Connection = conn;
                command.Connection.Open();
                command.CommandText = npgsqlDeleteString;

                NpgsqlParameter idParam = new NpgsqlParameter("@id", id);
                command.Parameters.Add(idParam);
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }
        catch (Exception ex)
        {
            err.ErrorMessage = ex.Message.ToString();
            throw;
        }
    }

    public Mails GetMail(int ID)
    {
        try
        {
            if (mailList == null)
            {
                mailList = GetMails();
            }
            foreach (Mails mail in mailList)
            {
                if (mail.Id == ID)
                {
                    return mail;
                }
            }
            return null;
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.ToString());
            err.ErrorMessage = ex.Message.ToString();
            throw;
        }
    }

    private List<Mails> GetMails()
    {
        try
        {
            using (conn)
            {
                mailList = new List<Mails>();

                conn = new NpgsqlConnection(connString);
                string npgsqlSelectString = "SELECT * FROM mails";
                command = new NpgsqlCommand(npgsqlSelectString, conn);
                command.Connection.Open();

                NpgsqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Mails mail = new Mails();
                    mail.Id = (int)reader[0];
                    mail.Id_entete = (int)reader[1];
                    mail.Emmetteur = reader[2].ToString().Replace(" ", "");
                    mail.Destinataires = reader[3].ToString().Replace(" ", "");
                    mail.Objet = reader[4].ToString().Replace(" ", "");
                    mail.Contenu = reader[5].ToString().Replace(" ", "");
                    mail.Date_envoi = reader[6].ToString().Replace(" ", "");
                    mail.Heure_envoi = reader[7].ToString().Replace(" ", "");
                    mailList.Add(mail);
                }
                command.Connection.Close();
                return mailList;
            }
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.ToString());
            err.ErrorMessage = ex.Message.ToString();
            throw;
        }
    }

    public string GetException()
    {
        return err.ErrorMessage.ToString();
    }
}


So, who can I do for call the function ProcessRequest(HttpContext context) ? Thank you for your help and sorry for my bad english ... ! Smile | :)
QuestionTextbox updation Pin
Avinash.Banda14-Nov-16 0:24
Avinash.Banda14-Nov-16 0:24 
AnswerRe: Textbox updation Pin
OriginalGriff14-Nov-16 0:31
mveOriginalGriff14-Nov-16 0:31 
GeneralRe: Textbox updation Pin
Avinash.Banda14-Nov-16 0:36
Avinash.Banda14-Nov-16 0:36 
GeneralRe: Textbox updation Pin
OriginalGriff14-Nov-16 0:39
mveOriginalGriff14-Nov-16 0:39 
GeneralRe: Textbox updation Pin
Avinash.Banda14-Nov-16 0:43
Avinash.Banda14-Nov-16 0:43 
GeneralRe: Textbox updation Pin
OriginalGriff14-Nov-16 0:45
mveOriginalGriff14-Nov-16 0:45 
GeneralRe: Textbox updation Pin
Avinash.Banda14-Nov-16 0:52
Avinash.Banda14-Nov-16 0:52 
GeneralRe: Textbox updation Pin
OriginalGriff14-Nov-16 1:03
mveOriginalGriff14-Nov-16 1:03 
GeneralRe: Textbox updation Pin
Avinash.Banda14-Nov-16 1:04
Avinash.Banda14-Nov-16 1:04 
QuestionCan I send Push Notifications to mobile phones from Windows Forms Application? Pin
Onur ERYILMAZ13-Nov-16 23:58
Onur ERYILMAZ13-Nov-16 23:58 
AnswerRe: Can I send Push Notifications to mobile phones from Windows Forms Application? Pin
Eddy Vluggen14-Nov-16 3:51
professionalEddy Vluggen14-Nov-16 3:51 
QuestionFile Transfer Queue Architecture Pin
Kevin Marois10-Nov-16 6:16
professionalKevin Marois10-Nov-16 6:16 
QuestionRe: File Transfer Queue Architecture Pin
Gerry Schmitz10-Nov-16 6:41
mveGerry Schmitz10-Nov-16 6:41 
AnswerRe: File Transfer Queue Architecture Pin
Kevin Marois10-Nov-16 6:42
professionalKevin Marois10-Nov-16 6:42 
GeneralRe: File Transfer Queue Architecture Pin
Gerry Schmitz10-Nov-16 7:04
mveGerry Schmitz10-Nov-16 7:04 
GeneralRe: File Transfer Queue Architecture Pin
Kevin Marois10-Nov-16 7:22
professionalKevin Marois10-Nov-16 7:22 
GeneralRe: File Transfer Queue Architecture Pin
Gerry Schmitz10-Nov-16 7:33
mveGerry Schmitz10-Nov-16 7:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.