1

I am working on website and I want upload data to a database that I have connected to in app.aspx.cs

I am encountering the below error every time I start the app:

Error CS0149 Method name expected PoshaWeb C:\Users\zatoo\Desktop\Pusha\workweb\PoshaWeb\App.aspx.cs 35 Active

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;

namespace PoshaWeb
{
    public partial class App : System.Web.UI.Page
    {   
        protected void Page_Load(object sender, EventArgs e)
        {

            Stream body = Context.Request.InputStream;
            System.Text.Encoding Encoding = Context.Request.ContentEncoding;
            StreamReader reader = new StreamReader(body, Encoding);
            String inJson = reader.ReadToEnd();
            var s = Newtonsoft.Json.JsonConvert.DeserializeObject(inJson);
            System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["MyPoshDB"].ConnectionString);
            Conn.Open();

            System.Data.SqlClient.SqlCommand Comm = new System.Data.SqlClient.SqlCommand
            {
                Connection = Conn
            };
            try
            {
                if (s("type").ToString = "addstudent")
                {
                    Comm.CommandText = "INSERT INTO Members (MemName, MemMobile, MemEmail, MemID, MemGovID, MemDocURL, MemPicURL) VALUES(N'" + s("name").ToString + "', N'"+ s("mobile").ToString + "', N'" + s("email").ToString + "', N'" + s("recordnumber").ToString + "', N'" + s("idnumber").ToString + "', N'" + s("picturepath").ToString + "', N'" + s("documentpath").ToString + "')";
                    Comm.ExecuteNonQuery();
                    Response.Write("{\"result\":\"success\"}");
                }
            }
            catch (Exception ex)
            {
                Page.Response.Write("{\"result\":\"fail\",\"desc\":" + Newtonsoft.Json.JsonConvert.SerializeObject(ex.Message) + "}");
            }
            Conn.Close();
        }

    }
}

The problem I have is that the variable s is not working inside the if statement.

Diado
  • 2,229
  • 3
  • 18
  • 21
  • 1
    FYI There isn't an a in JSON – Liam Aug 01 '18 at 09:43
  • also `if (s("type").ToString = "addstudent")` should be `if (s("type").ToString == "addstudent")` – Sasha Aug 01 '18 at 09:46
  • 1
    This whole thing is kinda messed up. You can't just write to the response of a web form like that, not in PageLoad anyway. It's very unclear what you want to do here – Liam Aug 01 '18 at 09:46
  • 1
    You should also read about `using` and why you need to use it. [Memory Leaks C#](https://stackoverflow.com/questions/5020814/memory-leaks-c-sharp) – Liam Aug 01 '18 at 09:47
  • Consider using the `using` statement to dispose your `SqlConnection` and `SqlCommand` - see https://stackoverflow.com/questions/75401/what-are-the-uses-of-using-in-c-sharp – Rui Jarimba Aug 01 '18 at 09:48
  • (without wanting to be overly critical) there really is more wrong with this code than right. The best advise I can offer is to read more on how this all works, etc and start again – Liam Aug 01 '18 at 09:50
  • You should also use a parameterized query. You are vulnerable to SQL Injection. – Palle Due Aug 01 '18 at 10:51

1 Answers1

0

As others stated, you could do with reading up using the links provided then go at this again. The fix to your issue is because you're using a method group rather than invoking the actual method call.

if (s("type").ToString = "addstudent")

Should be

if (s("type").ToString() == "addstudent")

This will solve the issue you're having, but there are more issues in your code than just this.

Sasha
  • 1,674
  • 1
  • 16
  • 23