Home > Back-end >  Parser errors not shown in asp.net
Parser errors not shown in asp.net

Time:11-06

recently, I'm working on the asp.net c# framework, I had a problem with the insert query but I can't see the parser error it shows a blank page. That's why I can't figure out the problem with my code. here it's my code and my web.config file.

web.config file:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  https://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>

  <system.web>

      <compilation  targetFramework="4.7.2" />
      <!-- ******************** -->
      
  </system.web>

  <system.codedom>
    <compilers>
      <compiler extension=".cs" language="c#;cs;csharp" warningLevel="4" compilerOptions="/langversion:7.3 /nowarn:1659;1699;1701;612;618" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      <compiler extension=".vb" language="vb;vbs;visualbasic;vbscript" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer " type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </compilers>
  </system.codedom>
</configuration>

the code of insert query:

sql = "INSERT INTO [transaction] (employeeName, receivedDate, recipient, senderParty, receivedParty, tranNum, email, status, userID)"  
             "values (@employName, @recDate, @recipientName, @sendName, @recName, @tranNumber, @employEmail, @stat, @id)";
            //Response.Write(reciveDate.Value);
            //Response.Write(empEmail);

            using (SqlCommand cmd = new SqlCommand(sql, conn))
            {
                if (conn.State == ConnectionState.Closed)
                conn.Open();
                cmd.Parameters.AddWithValue("@employName", employeeName);
                cmd.Parameters.AddWithValue("@recDate", reciveDate.Value);
                cmd.Parameters.AddWithValue("@recipientName", reciever.Value); ;
                cmd.Parameters.AddWithValue("@sendName", senderAdress.Value);
                cmd.Parameters.AddWithValue("@recName", recieverAdress.Value);
                cmd.Parameters.AddWithValue("@tranNumber", Convert.ToInt32(tranID.Value));
                cmd.Parameters.AddWithValue("@employEmail", empEmail);
                cmd.Parameters.AddWithValue("@stat", "p");
                cmd.Parameters.AddWithValue("@id", empID);
                a = cmd.ExecuteNonQuery();
            }

I tried to insert data into the Microsoft SQL Management server, but it didn't work and I can't see the parser error.

How I can display the parser error?

please help me, thanks

CodePudding user response:

you need to put try catch and see the the exception.

you also need to first run your query directly in SQL server and see if there is anything wrong with your query.

and I am not sure but your query is incorrect here. it is producing the below query.

INSERT INTO [transaction] (employeeName, receivedDate, recipient, senderParty, receivedParty, tranNum, email, status, userID)values (@employName, @recDate, @recipientName, @sendName, @recName, @tranNumber, @employEmail, @stat, @id)

if you look it carefully there is no space before values. so you need to correct it.

and please declare con with using statement.

CodePudding user response:

as noted, you should have a space before "values".

or use @ for the string, and use a line break.

And you should STRONG type the parameters.

(don't use addwith)

Thus:

sql = @"INSERT INTO [transaction] 
(employeeName, receivedDate, recipient, senderParty, receivedParty, tranNum, email, status, userID)
VALUES (@employName, @recDate, @recipientName, @sendName, @recName, @tranNumber, @employEmail, @stat, @id)";

using (SqlCommand cmd = new SqlCommand(sql))
{
    cmd.Parameters.Add("@employName", SqlDbType.NVarChar).Value = employeeName;
    cmd.Parameters.Add("@recDate", SqlDbType.Date).Value = reciveDate.Value;
    cmd.Parameters.Add("@recipientName", SqlDbType.NVarChar).Value = reciever.Value;
    cmd.Parameters.Add("@sendName", SqlDbType.NVarChar).Value = senderAdress.Value;
    cmd.Parameters.Add("@recName", SqlDbType.NVarChar).Value = recieverAdress.Value;
    cmd.Parameters.Add("@tranNumber", SqlDbType.Int).Value = Convert.ToInt32(tranID.Value);
    cmd.Parameters.Add("@employEmail", SqlDbType.NVarChar).Value = empEmail;
    cmd.Parameters.Add("@stat", SqlDbType.NVarChar).Value = "p";
    cmd.Parameters.Add("@id", SqlDbType.Int).Value = empID;
    cmd.ExecuteNonQuery();
}

And where are the variables coming from?

If those are text boxes, then you are to use MyAddress.Text, and not MyAddress.Value

  • Related