locked
Conver this below code to asp.net 2.0 c# RRS feed

  • Question

  • User-807418713 posted
    Hi

    Please Convert This Code To Old ASP.NET 2.0 C#

    IT would be needful

    Aspx.cs:
    [WebMethod]


    public static void Insert_Data(List<MyModel> myModels)
    {

    foreach (var item in myModels)
    {
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr527"].ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("Insert into Personal_Details (ItemName,Rate,Quantity) values(@ItemName,@Rate,@Quantity)", conn);
    cmd.Parameters.AddWithValue("@ItemName", item.ItemName);

    cmd.Parameters.AddWithValue("@Rate", item.Rate);

    cmd.Parameters.AddWithValue("@Quantity", item.Quantity);

    cmd.ExecuteNonQuery();

    conn.Close();
    }

    }

    public class MyModel
    {
    public string ItemName { get; set; }
    public string Rate { get; set; }
    public string Quantity { get; set; }
    }
    Thanks
    Tuesday, June 4, 2019 4:49 AM

Answers

  • User665608656 posted

    Hi Gopi,

    According to your description and error warning, the key is that the value of the property in myModels object you passed from the front desk is null.

    To solve this issue you should change something in MyModel entity classes you created.

    First, you could better to set the three field modifiers in the MyModel entity class to be private.

    You could refer to  https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/private

    And most importantly, you made an error in assigning values to three fields in the MyModel entity class. The right thing to do is assign values to fields instead of assigning fields to value.

    Specifically, you could refer to https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/set

    The changed code is as follows:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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.Collections.Generic;
    using System.Web.Services;
    using System.Configuration;
    
    
    public partial class Default3 : System.Web.UI.Page
    {
    
        [WebMethod]
    
        public static void InsertData(List<MyModel> myModels)
        {
    
            foreach (MyModel item in myModels)
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["IConnectionString"].ConnectionString);
                conn.Open();
                SqlCommand cmd = new SqlCommand("Insert into Arrival (ItemName,Rate,Quantity) values(@ItemName,@Rate,@Quantity)", conn);
    
                cmd.Parameters.AddWithValue("@ItemName", item.ItemName);
                cmd.Parameters.AddWithValue("@Rate", item.Rate);
                cmd.Parameters.AddWithValue("@Quantity", item.Quantity);
    
                cmd.ExecuteNonQuery();
                conn.Close();
    
    
    
            }
        }
    
          public class MyModel
        {
            private string itemname;
            private string rate;
            private string quantity;
            public string ItemName
            {
                get
                {
                    return itemname;
                }
                set
                {
                    itemname = value;
                }
            }
    
            public string Rate
            {
                get
                {
                    return rate;
                }
                set
                {
                    rate = value;
                }
            }
    
            public string Quantity
            {
                get
                {
                    return quantity;
                }
                set
                {
                    quantity = value;
                }
            }
        }

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 5, 2019 6:05 AM

All replies

  • User753101303 posted

    Hi,

    Always be explicit. It seems you had WebMethod in 2.0. Ah could it be because of auto implemented properties in which case you need something such as :

    private string _ItemName;
    public string ItemName{get{return _ItemName;} set {_ItemName=value}}; 

    But as this is compiler trick you might be able to use that even with ASP.NET 2.0 (make also sure you still want to target this outdated version).

    Tuesday, June 4, 2019 8:19 AM
  • User-807418713 posted
    Hi
    foreach (var item in myModels)
    {

    How to write this above code var not support inin asp.net 2.0 c#
    Tuesday, June 4, 2019 9:48 AM
  • User475983607 posted

    Hi
    foreach (var item in myModels)
    {

    How to write this above code var not support inin asp.net 2.0 c#

    Simply replace var with the the actual type.  

    foreach (MyModel item in myModels)
    {

    I recommend reading  the C# programming guide and reference documents when you are not sure about the language constructs.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var

    Tuesday, June 4, 2019 10:01 AM
  • User665608656 posted

    Hi Gopi,

    According to your description, if you want to use ASP.NET 2.0 to display your code, you need to change the var of your foreach loop to MyModel type.

    Because var has been used since ASP.NET 3.0, you could refer to this link:

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var

    Here is the code that needs to be modified in ASP.NET 2.0:

    foreach (MyModel item in myModels)
    {
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["constr527"].ConnectionString);
    conn.Open();
    SqlCommand cmd = new SqlCommand("Insert into Personal_Details (ItemName,Rate,Quantity) values(@ItemName,@Rate,@Quantity)", conn);
    cmd.Parameters.AddWithValue("@ItemName", item.ItemName);
    
    cmd.Parameters.AddWithValue("@Rate", item.Rate);
    
    cmd.Parameters.AddWithValue("@Quantity", item.Quantity);
    
    cmd.ExecuteNonQuery();
    
    conn.Close();
    }

    Best Regards,

    YongQing.

    Tuesday, June 4, 2019 10:01 AM
  • User-807418713 posted
    Hello

    How would be this code

    public class MyModel
    {
    public string ItemName { get; set; }
    public string Rate { get; set; }
    public string Quantity { get; set; }
    }
    Thanks

    Can u give my code in complete asp. Net 2.0 c#
    Tuesday, June 4, 2019 10:07 AM
  • User475983607 posted

    Gopi.MCA

    Hello

    How would be this code

    public class MyModel
    {
    public string ItemName { get; set; }
    public string Rate { get; set; }
    public string Quantity { get; set; }
    }
    Thanks

    Can u give my code in complete asp. Net 2.0 c#

    I strongly recommend that you take a few minutes to learn how to use the online support references.   The docs illustrate the language constructs.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-properties

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties

    Essentially use a backing member.  This concept is covered in the C# programming guide.

    public class MyModel
        {
            private string itemName;
            private string rate;
            private string quantity;
    
            public string ItemName
            {
                get
                {
                    return itemName;
                }
                set
                {
                    itemName = value;
                }
            }
    
            public string Rate
            {
                get
                {
                    return rate;
                }
                set
                {
                    rate = value
                }
            }
    
            public string Quantity
            {
                get
                {
                    return quantity;
                }
                set
                {
                   quantity = value;
                }
            }
        }

    Edit: fixed code

    Tuesday, June 4, 2019 10:38 AM
  • User-807418713 posted

    Hi Mgebhard

    Thanks For Your Code

    Hi

    I'm getting this error

    ExceptionType: "System.Data.SqlClient.SqlException"
    Message: "The parameterized query '(@ItemName nvarchar(4000),@Rate nvarchar(4000),@Quantity nvarcha' expects the parameter '@ItemName', which was not supplied."
    StackTrace: "   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
    ↵   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
    ↵   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
    ↵   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
    ↵   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
    ↵   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
    ↵   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
    ↵   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
    ↵   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
    ↵   at New_Item_entry.InsertData(List`1 myModels)"

    Here is my aspx cs code

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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.Collections.Generic;
    using System.Web.Services;
    using System.Configuration;
    
    
    public partial class Default3 : System.Web.UI.Page
    {
    
        [WebMethod]
    
        public static void InsertData(List<MyModel> myModels)
        {
    
            foreach (MyModel item in myModels)
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["IConnectionString"].ConnectionString);
                conn.Open();
                SqlCommand cmd = new SqlCommand("Insert into Arrival (ItemName,Rate,Quantity) values(@ItemName,@Rate,@Quantity)", conn);
    
                cmd.Parameters.AddWithValue("@ItemName", item.ItemName);
                cmd.Parameters.AddWithValue("@Rate", item.Rate);
                cmd.Parameters.AddWithValue("@Quantity", item.Quantity);
    
                cmd.ExecuteNonQuery();
                conn.Close();
    
    
    
            }
        }
    
        public class MyModel
        {
            public string itemName;
            public string rate;
            public string quantity;
    
            public string ItemName
            {
                get
                {
                    return itemName;
                }
                set
                {
                    value = itemName;
                }
            }
    
            public string Rate
            {
                get
                {
                    return rate;
                }
                set
                {
                    value = rate;
                }
            }
    
            public string Quantity
            {
                get
                {
                    return quantity;
                }
                set
                {
                    value = quantity;
                }
            }
        }

    Please let me know where i'm wrong

    Tuesday, June 4, 2019 11:51 AM
  • User475983607 posted

    Please let me know where i'm wrong

    The class you posted clearly does not match the class I posted but that is besides the point.

    The error message is abundantly clear.  The parameter query expects input parameters but parameters where not passed.  This can be due to null values.  Most likely the client code that call the web method has issues.  

    Please learn how to use the Visual Studio debugger to troubleshoot code.

    https://docs.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2019

    Please use the browser's dev tools (F12) to troubleshoot JavaScript.

    Tuesday, June 4, 2019 12:46 PM
  • User-1038772411 posted

    Hello Gopi.MCA,

    Try with this

    you must check for null value,If it null you must pass like this

    cmd.Parameters.AddWithValue("@ItemName", item.ItemName ?? (object)DBNull.Value);
    cmd.Parameters.AddWithValue("@Rate", item.Rate ?? (object)DBNull.Value);
    cmd.Parameters.AddWithValue("@Quantity", item.Quantity ?? (object)DBNull.Value);

    or implement like this

    SqlParameter unitsParam = command.Parameters.AddWithValue("@ItemName", item.ItemName);
    if (item.ItemName== null)
    {
        item.ItemName.Value = DBNull.Value;
    }

    I hope this will help you to solve your problem

    Thank you

    Wednesday, June 5, 2019 6:00 AM
  • User665608656 posted

    Hi Gopi,

    According to your description and error warning, the key is that the value of the property in myModels object you passed from the front desk is null.

    To solve this issue you should change something in MyModel entity classes you created.

    First, you could better to set the three field modifiers in the MyModel entity class to be private.

    You could refer to  https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/private

    And most importantly, you made an error in assigning values to three fields in the MyModel entity class. The right thing to do is assign values to fields instead of assigning fields to value.

    Specifically, you could refer to https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/set

    The changed code is as follows:

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    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.Collections.Generic;
    using System.Web.Services;
    using System.Configuration;
    
    
    public partial class Default3 : System.Web.UI.Page
    {
    
        [WebMethod]
    
        public static void InsertData(List<MyModel> myModels)
        {
    
            foreach (MyModel item in myModels)
            {
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["IConnectionString"].ConnectionString);
                conn.Open();
                SqlCommand cmd = new SqlCommand("Insert into Arrival (ItemName,Rate,Quantity) values(@ItemName,@Rate,@Quantity)", conn);
    
                cmd.Parameters.AddWithValue("@ItemName", item.ItemName);
                cmd.Parameters.AddWithValue("@Rate", item.Rate);
                cmd.Parameters.AddWithValue("@Quantity", item.Quantity);
    
                cmd.ExecuteNonQuery();
                conn.Close();
    
    
    
            }
        }
    
          public class MyModel
        {
            private string itemname;
            private string rate;
            private string quantity;
            public string ItemName
            {
                get
                {
                    return itemname;
                }
                set
                {
                    itemname = value;
                }
            }
    
            public string Rate
            {
                get
                {
                    return rate;
                }
                set
                {
                    rate = value;
                }
            }
    
            public string Quantity
            {
                get
                {
                    return quantity;
                }
                set
                {
                    quantity = value;
                }
            }
        }

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 5, 2019 6:05 AM