2

In my android application,my database is over MS SQL server. And to access that database, I am creating a c# script and calling from my app same as php.My activity is a login activity where user enters his Username and Password, then on button click, that c# script is calling to check that username and password is valid or not. If valid, then will send that user's data in JSONArray format and if Invalid, then will send a code=0 in json format. But I don't know how to send JSON data from c# to android like php. Here is my c# code below whatever I wrote. Please help me how to send JSON data from MS SQL and c# to Android.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LoginForm
{
    public class LoginCheck
    {
        string strUser=Request.Querystring["strUser"];//getting username from activity
        string strPassword=Request.Querystring["strPassword"];//getting password from activity

        try
        {
            SqlDataReader myReader = null;
            SqlConnection conn = new SqlConnection("Data Source=***.**.**.*; Initial Catalog=GPSDB;Persist Security Info=True;User ID=##;Password=##$$#@!@!#");
                    conn.Open();
            SqlCommand check = new SqlCommand("select * from Table_User where strUser=@strUser AND strPassword=@strPassword;", conn);
            check.Parameters.AddWithValue("@strUser",strUser);
            check.Parameters.AddWithValue("@strPassword",strPassword);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if(dt.Rows.Count>0)
            {
                //JSON code...
            }    
        }
    }
}
  • Take look at the `JavascriptSerilizer Class` [MSDN](http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx) and also there is already lot of answers if you search you will get it like [here](http://stackoverflow.com/questions/14088928/javascriptserializer-serialize-a-list-a-string-into-json) is one. Try it and if you got any issues let us know –  Sep 24 '14 at 08:19
  • But I want to send this data to android app. I am only using c# file not .aspx file. –  Sep 24 '14 at 08:21
  • What do you mean you are using only C# file. Is it web service ? –  Sep 24 '14 at 08:23
  • yes,its a web service like php..!!! –  Sep 24 '14 at 08:25
  • Then you are going to make it `RESTFul` service I assume if that's the case then simple you have to define properties of method to make it restful [Here](http://msdn.microsoft.com/en-in/library/dd203052.aspx) is the link. Then you need to serialize the data and return it. Check these answers for reference [1](http://stackoverflow.com/questions/17398019/how-to-convert-datatable-to-json-in-c-sharp) and [2](http://stackoverflow.com/questions/17928513/whats-the-best-way-to-json-serialize-a-net-datatable-in-wcf) –  Sep 24 '14 at 08:29
  • Read carefully and try it ! And explain in detail what exactly you are trying to do. I am not sure what you are trying to do ? –  Sep 24 '14 at 08:30
  • I am trying to get data from database and then send them in JSON format to android application same as php. That's it...!!! –  Sep 24 '14 at 08:39
  • 1) From Where you are trying to get data ? 2) What are you using to get the data ? 3) If web service will it be SOAP or `Rest` ? and more info with the code of what have you tried –  Sep 24 '14 at 08:41
  • from MS Sql server database. if username and password is correct then, will retrieve data from MS SQL Server database according to that username and then send that data to android app in JSON format. –  Sep 24 '14 at 08:43
  • I think you should use Newtonsoft.Json to generate the json you want. and before that, maybe you should define a class to wrap your result. – Gavin Fang Sep 24 '14 at 08:55

1 Answers1

1

The code you posted isn't even syntactically correct. You'll probably need to learn more C#. Just saying.

For your service you'll need something you can actually host. The way to go today would be a REST service implemented with ASP.NET MVC 4 web api controllers. See http://www.asp.net/web-api

With that you can simply put your data into strong-typed DTOs and return them. Web Api will then serialize them for you depending on the request headers to e.g. XML or JSON.

bstenzel
  • 1,231
  • 9
  • 14