[Solved] ASP.Net Error: No mapping exists from object type System.Collections.Generic.List`1[[System.String]] to a known managed provider native type

lingers
 
on Aug 23, 2021 06:45 AM
Sample_127139.zip
4222 Views

i have this GridView which has selected tagit text box. I want to prevent submitting of the GridView if a number appears in more than one tagit textbox that is if a number is selected more than once in selected tagit textbox, it shouldn't submit, it should display error in a label and not submit.

But i am having this error Message

Server Error in '/WebSite6' Application.
No mapping exists from object type System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to a known managed provider native type.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: System.ArgumentException: No mapping exists from object type System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to a known managed provider native type.
 
Source Error:
 
 
Line 107:         cmd.Parameters.AddWithValue("@accepted", selectedTags);
Line 108:         con.Open();
Line 109:         cmd.ExecuteNonQuery();
Line 110:         con.Close();
Line 111:     }
 
Source File: c:\Users\hp\Documents\Visual Studio 2010\WebSites\WebSite6\Rec2.aspx.cs    Line: 109

 

public partial class Rec2 : System.Web.UI.Page
{
    SqlCommand cmd1 = new SqlCommand();
    SqlCommand cmd = new SqlCommand();
 
    SqlConnection dbConn = new SqlConnection();
    SqlConnection dbConn1 = new SqlConnection();
    SqlConnection dbConn2 = new SqlConnection();
    SqlDataReader dr, dr2, dr1, dr3;
 
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!this.IsPostBack)
        {
            GridView1.DataSource = GetData();
            GridView1.DataBind();
        } 
 
        foreach (GridViewRow row in GridView1.Rows)
        {
            foreach (TableCell cell in row.Cells)
            {
                cell.Attributes.CssStyle["text-align"] = "center";
            }
        }
        //  txtdate.Text = DateTime.Today.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
    }
 
    [WebMethod]
    public static string[] GetCustomers()
    {
        List<string> customers = new List<string>();
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "SELECT baseno FROM assets WHERE baseno NOT IN (SELECT no FROM stock)UNION ALL SELECT no FROM stock WHERE Active ='2'";
                cmd.Connection = conn;
                conn.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        customers.Add(sdr["baseno"].ToString());
                    }
                }
                conn.Close();
            }
        }
 
        return customers.ToArray();
    }
 
    protected void OnUpdate(object sender, EventArgs e)
    {          
        foreach (GridViewRow row in GridView1.Rows)
        {          
            if (Session["tt"].ToString() == "3")
            {
                List<string> selectedTags = hfCustomerIds.Value.Split(',').ToList();
                List<string> duplicates = selectedTags.GroupBy(x => x).SelectMany(g => g.Skip(1)).ToList();
                if (duplicates.Count == 0)
                { 
                    string orderid = row.Cells[0].Text;
                    string deliverydate = (row.FindControl("txtdate") as TextBox).Text.Trim();
                    string waybillno = (row.FindControl("txtwaybill") as TextBox).Text.Trim();
                    string comment = (row.FindControl("txtcomment") as TextBox).Text.Trim();
                    string qtyreceived = (row.FindControl("txtcomment1") as TextBox).Text.Trim();
                    // string selectedTags = (row.FindControl("hfIds") as HiddenField).Value.Trim().Replace(",", "/");
 
                    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                    using (SqlConnection con = new SqlConnection(constr))
                    {
                        string query = "UPDATE orders SET deliverydate=@deliverydate, qtyreceived=@qtyreceived ,waybillno=@waybillno,comment=@comment,clicked ='2' WHERE orderid=@orderid ";
                        query += " INSERT INTO orders_cylinder (orderid,baseno) VALUES (@orderid,@accepted)";
                        using (SqlCommand cmd = new SqlCommand())
                        {
                            cmd.CommandText = query;
                            cmd.Connection = con;
                            cmd.CommandType = CommandType.Text;
                            cmd.Parameters.AddWithValue("@orderid", orderid);
                            cmd.Parameters.AddWithValue("@deliverydate", deliverydate);
                            cmd.Parameters.AddWithValue("@waybillno", waybillno);
                            cmd.Parameters.AddWithValue("@comment", comment);
                            cmd.Parameters.AddWithValue("@qtyreceived", qtyreceived);
                            cmd.Parameters.AddWithValue("@accepted", selectedTags);
                            con.Open();
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                    }
 
 
                    Response.Redirect("Conf.aspx");
                    //  Response.Redirect("Conf1.aspx");
                }
                else
                {
                    Response.Redirect("Conf2.aspx");
                }                
            }            
    }
 
    private DataTable GetData()
    {
        string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        string query = "SELECT * FROM orders  where purchaseorderno='" + Session["po"].ToString() + "'";
        using (SqlConnection con = new SqlConnection(conString))
        {
            SqlCommand cmd = new SqlCommand(query);
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
 
                    return dt;
                }
            }
        }
    }    
}
 

please help me

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 27, 2021 05:55 AM
on Feb 08, 2022 10:33 AM

I have verified your code. You need to change the JavaScript code.

Rest code is ok.

Refer below code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

You need to modify the code as per your columns in the table.

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/tag-it/2.0/css/jquery.tagit.min.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/tag-it/2.0/css/tagit.ui-zendesk.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/tag-it/2.0/js/tag-it.min.js"></script>
<script type="text/javascript">
    $(function () {
        $.ajax({
            url: "<%=ResolveUrl("Default.aspx/GetCustomers")%>",
            data: "{}",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                var sampleTags = [];
                $.each(data.d, function (i, item) {
                    sampleTags.push(item);
                });
                $(".myTags").tagit({
                    availableTags: sampleTags
                });
            },

            error: function (response) {
                alert(response.responseText);
            },
            failure: function (response) {
                alert(response.responseText);
            }
        });
    });
    $(document).on('click', '[id*=btnSave]', function () {
        var ids = [];
        $.each($('[id*=GridView1] tr:has(td)'), function () {
            var currentIds = [];
            $.each($(this).find('.tagit-choice'), function () {
                currentIds.push($(this).find('input[type=hidden]').val());
            });
            ids.push(currentIds.join(','));
        });
        $('[id*=hfCustomerIds]').val(ids.join('|'));
    });
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Font-Names="Candara" Width="1300px">
    <Columns>
        <asp:BoundField DataField="orderid" HeaderText="id" ItemStyle-Width="30" />
        <asp:BoundField DataField="purchaseorderno" HeaderText="PO NO" ItemStyle-Width="100" />
        <asp:BoundField DataField="quantity" HeaderText="Quantity" />
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate>
                <div align="center">
                    <asp:TextBox ID="txtdate" runat="server" Height="38" Width="200" Text='<%#DateTime.Today.ToString("MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture) %>'></asp:TextBox>
                </div>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Waybill">
            <ItemTemplate>
                <asp:TextBox ID="txtwaybill" runat="server" Width="100" Height="38"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Comment">
            <ItemTemplate>
                <asp:TextBox ID="txtcomment" runat="server" Width="100" Height="38"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity Been Recieved">
            <ItemTemplate>
                <asp:TextBox ID="txtcomment1" runat="server" Width="100" Height="38"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Baseno" ItemStyle-Width="200" ItemStyle-Height="38">
            <ItemTemplate>
                <ul id="myTags" class="myTags">
                </ul>
                <asp:HiddenField ID="hfIds" runat="server" />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:HiddenField ID="hfCustomerIds" runat="server" />
<asp:Button ID="btnSave" Text="Save" runat="server" OnClick="OnUpdate" />

Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        GridView1.DataSource = GetData();
        GridView1.DataBind();
    }

    foreach (GridViewRow row in GridView1.Rows)
    {
        foreach (TableCell cell in row.Cells)
        {
            cell.Attributes.CssStyle["text-align"] = "center";
        }
    }
    //  txtdate.Text = DateTime.Today.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
}

[WebMethod]
public static string[] GetCustomers()
{
    List<string> customers = new List<string>();
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "SELECT DISTINCT CustomerID baseno FROM Orders";
            cmd.Connection = conn;
            conn.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(sdr["baseno"].ToString());
                }
            }
            conn.Close();
        }
    }

    return customers.ToArray();
}

protected void OnUpdate(object sender, EventArgs e)
{
    //if (Session["tt"].ToString() == "3")
    {
        List<string> tags = hfCustomerIds.Value.Split(new char[] { ',', '|' }).ToList();
        List<string> duplicates = tags.Where(x => x != "").GroupBy(x => x).SelectMany(g => g.Skip(1)).ToList();
        if (duplicates.Count == 0)
        {

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                string orderid = row.Cells[0].Text;
                string deliverydate = (row.FindControl("txtdate") as TextBox).Text.Trim();
                string waybillno = (row.FindControl("txtwaybill") as TextBox).Text.Trim();
                string comment = (row.FindControl("txtcomment") as TextBox).Text.Trim();
                string qtyreceived = (row.FindControl("txtcomment1") as TextBox).Text.Trim();
                // string selectedTags = (row.FindControl("hfIds") as HiddenField).Value.Trim().Replace(",", "/");
                string selectedTags = hfCustomerIds.Value.Split('|')[i];

                string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    string query = "UPDATE orders SET deliverydate=@deliverydate, qtyreceived=@qtyreceived ,waybillno=@waybillno,comment=@comment,clicked ='2' WHERE orderid=@orderid ";
                    query += " INSERT INTO orders_cylinder (orderid,baseno) VALUES (@orderid,@accepted)";
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = query;
                        cmd.Connection = con;
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("@orderid", orderid);
                        cmd.Parameters.AddWithValue("@deliverydate", deliverydate);
                        cmd.Parameters.AddWithValue("@waybillno", waybillno);
                        cmd.Parameters.AddWithValue("@comment", comment);
                        cmd.Parameters.AddWithValue("@qtyreceived", qtyreceived);
                        cmd.Parameters.AddWithValue("@accepted", selectedTags);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }

            Response.Redirect("1.aspx");
        }
        else
        {
            Response.Redirect("2.aspx");
        }

    }

}
private DataTable GetData()
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string query = "SELECT TOP 4 EmployeeId orderid,OrderId purchaseorderno,ShipVia quantity FROM orders";
    using (SqlConnection con = new SqlConnection(conString))
    {
        SqlCommand cmd = new SqlCommand(query);
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataTable dt = new DataTable())
            {
                sda.Fill(dt);

                return dt;
            }
        }
    }
}

Screenshot