Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I do the project but i have some ERROR!!!

No mapping exists from object type System.Web.UI.WebControls.ListItem to a known managed provider native type.

this my c# code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class AddMember : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Camt_LibraryConnectionString"].ConnectionString);

            conn.Open();
            string checkuser = "select count(*) from tbl_member where pre_name='" + ddPreN.Text + "'";
            SqlCommand com = new SqlCommand(checkuser, conn);
            int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
            conn.Close();
            if (temp == 1)
            {
                Response.Write("User already Exists");
            }




        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Camt_LibraryConnectionString"].ConnectionString);

            conn.Open();
            string insertQuery = "insert into tbl_member (pre_name,name,member_type_id,dept_id,student_id,address,phone,mobile,create_date,expire_date,image,debt) values (@Pname ,@Name ,@MemberTy ,@Dept, @StudentID, @Adderss, @Phone, @Mobile, @Cdate, @Edate, @Image, @Debt)";
            SqlCommand com = new SqlCommand(insertQuery, conn);

            com.Parameters.AddWithValue("@Pname", ddPreN.SelectedItem.ToString());
            com.Parameters.AddWithValue("@Name", TextBoxName.Text);
            com.Parameters.AddWithValue("@MemberTy", ddMember_Type.SelectedItem);
            com.Parameters.AddWithValue("@Dept", ddDept.SelectedItem.ToString());
            com.Parameters.AddWithValue("@StudentID", TextBoxStudenID.Text);
            com.Parameters.AddWithValue("@Adderss", TextBoxAddress.Text);
            com.Parameters.AddWithValue("@Phone", TextBoxcall.Text);
            com.Parameters.AddWithValue("@Mobile", TextBoxMobie.Text);
            com.Parameters.AddWithValue("@Cdate", DatePicker1.DateFormat);
            com.Parameters.AddWithValue("@Edate", DatePicker2.DateFormat);
            com.Parameters.AddWithValue("@Image", File_UploadImage.FileName);
            com.Parameters.AddWithValue("@Debt", TextBoxDebt.Text);

            com.ExecuteNonQuery();
             conn.Close();
            try 
{
    File_UploadImage.SaveAs(@"C:\Users\ATOMMIK\Documents\Visual Studio 2013\WebSites\Weblibrary0.1\Uploads" + File_UploadImage.FileName); 
} 
catch (Exception ex) 
{ 
Response.Write(ex.ToString()); 
} 

            Response.Write("Reqisteration is submit");

          
        }
        catch (Exception ex)
        {
            Response.Write("Error:" + ex.ToString());
        }
    }
}



and this HTML code

ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddMember.aspx.cs" Inherits="AddMember" %>

<%@ Register assembly="DatePickerControl" namespace="DatePickerControl" tagprefix="cc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
        .auto-style2 {
            text-align: right;
            width: 175px;
        }
        .auto-style3 {
            width: 175px;
            text-align: right;
        }
        .auto-style4 {
            text-align: right;
            width: 175px;
            height: 26px;
        }
        .auto-style5 {
            height: 26px;
        }
        .auto-style6 {
            width: 185px;
        }
        .auto-style7 {
            height: 16px;
            width: 185px;
        }
        .auto-style8 {
            height: 26px;
            width: 185px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">คำนำหน้านาม:</td>
                <td class="auto-style6">
                    <asp:DropDownList ID="ddPreN" runat="server" Width="260px">
                        <asp:ListItem>นาย</asp:ListItem>
                        <asp:ListItem>นาง</asp:ListItem>
                        <asp:ListItem>นางสาว</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style2">ชื่อ:</td>
                <td class="auto-style6">
                    <asp:TextBox ID="TextBoxName" runat="server" Width="260px"></asp:TextBox>
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style2">ประเภทสมาชิก:</td>
                 <td class="auto-style7">
                <asp:DropDownList ID="ddMember_Type" runat="server" DataSourceID="SqlDataSource1" DataTextField="member_type_name" DataValueField="member_type_name" Width="260px">
                </asp:DropDownList>
                     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Camt_LibraryConnectionString %>" SelectCommand="SELECT [member_type_name] FROM [tbl_member_type]"></asp:SqlDataSource>
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style2">สังกัดหน่วยงาน:</td>
                <td class="auto-style6"><asp:DropDownList ID="ddDept" runat="server" DataSourceID="SqlDataSource2" DataTextField="dept_name" DataValueField="dept_name" Width="260px">
                </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Camt_LibraryConnectionString %>" SelectCommand="SELECT [dept_name] FROM [tbl_dept]"></asp:SqlDataSource>
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style2">รหัสนักศึกษา:</td>
                <td class="auto-style6">
                    <asp:TextBox ID="TextBoxStudenID" runat="server" Width="260px"></asp:TextBox>
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style2">ที่อยู่:</td>
                <td class="auto-style6">
                    <asp:TextBox ID="TextBoxAddress" runat="server" Width="260px"></asp:TextBox>
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style4">โทรศัพท์:</td>
                <td class="auto-style8">
                    <asp:TextBox ID="TextBoxcall" runat="server" Width="260px"></asp:TextBox>
                </td>
                <td class="auto-style5"></td>
            </tr>
            <tr>
                <td class="auto-style2">มือถือ:</td>
                <td class="auto-style6">
                    <asp:TextBox ID="TextBoxMobie" runat="server" Width="260px"></asp:TextBox>
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style2">วันที่สมัคร:</td>
                <td class="auto-style6"> 
                    <cc1:DatePicker ID="DatePicker1"  runat="server" Width="280px" />
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style2">วันหมดอายุ:</td>
                <td class="auto-style6">
                    <cc1:DatePicker ID="DatePicker2"  runat="server" Width="280px" />
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style2">รูป:</td>
                <td class="auto-style6">
                <asp:FileUpload ID="File_UploadImage" runat="server" Width="280px" />

                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style3">หนี้สิน:</td>
                <td class="auto-style6">
                    <asp:TextBox ID="TextBoxDebt" runat="server" Width="260px"></asp:TextBox>
                </td>
                <td> </td>
            </tr>
            <tr>
                <td class="auto-style3"> </td>
                <td class="auto-style6">
                    <asp:Button ID="Button1" runat="server" Height="38px" OnClick="Button1_Click" Text="ADD" Width="79px" />
                </td>
                <td> </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>


PLEASE HELP !!! -- xx
Posted
Comments
ZurdoDev 24-Aug-15 8:34am    
Just post the relevant code. Which line of code gives that error?
Member 11287299 24-Aug-15 8:37am    
No have code that is error. But when i run the code it show me " Error:System.ArgumentException: No mapping exists from object type System.Web.UI.WebControls.ListItem to a known managed provider native type"

Start by fixing the SQL Injection[^] vulnerability in your Page_Load code:
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Camt_LibraryConnectionString"].ConnectionString))
        using (SqlCommand com = new SqlCommand("select count(*) from tbl_member where pre_name = @pre_name", conn))
        {
            com.Parameters.AddWithValue("@pre_name", ddPreN.Text);

            conn.Open();
            int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
            if (temp != 0)
            {
                Response.Write("User already Exists");
            }
        }
    }
}

Then, in your Button1_Click method, check the value you are passing to the @MemberTy parameter. You are passing ddMember_Type.SelectedItem, which is a System.Web.UI.WebControls.ListItem object. You need to pass its Value instead:
C#
protected void Button1_Click(object sender, EventArgs e)
{
    const string InsertQuery = @"insert into tbl_member (pre_name, name, member_type_id, dept_id, student_id, address, phone, mobile, create_date, expire_date, image,debt) values (@Pname, @Name, @MemberTy, @Dept, @StudentID, @Adderss, @Phone, @Mobile, @Cdate, @Edate, @Image, @Debt)";

    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Camt_LibraryConnectionString"].ConnectionString))
    using (SqlCommand com = new SqlCommand(InsertQuery, conn))
    {
        com.Parameters.AddWithValue("@Pname", ddPreN.SelectedItem.Value);
        com.Parameters.AddWithValue("@Name", TextBoxName.Text);
        com.Parameters.AddWithValue("@MemberTy", ddMember_Type.SelectedItem.Value);
        com.Parameters.AddWithValue("@Dept", ddDept.SelectedItem.Value);
        com.Parameters.AddWithValue("@StudentID", TextBoxStudenID.Text);
        com.Parameters.AddWithValue("@Adderss", TextBoxAddress.Text);
        com.Parameters.AddWithValue("@Phone", TextBoxcall.Text);
        com.Parameters.AddWithValue("@Mobile", TextBoxMobie.Text);

        // TODO: Pass the correct values here:
        com.Parameters.AddWithValue("@Cdate", DatePicker1.DateFormat);
        com.Parameters.AddWithValue("@Edate", DatePicker2.DateFormat);

        com.Parameters.AddWithValue("@Image", File_UploadImage.FileName);
        com.Parameters.AddWithValue("@Debt", TextBoxDebt.Text);

        conn.Open();
        com.ExecuteNonQuery();
    }

    string imagePath = Server.MapPath("~/Uploads/");
    string fileName = System.IO.Path.GetFileName(File_UploadImage.FileName);
    string filePath = System.IO.Path.Combine(imagePath, fileName);
    File_UploadImage.SaveAs(filePath);

    Response.Write("Reqisteration is submit");
}

You also need to check the value of your @Cdate and @Edate parameters - currently, you're passing the date format string, rather than the selected date.

I've also fixed your image saving code to use an app-relative path, rather than hard-coding the project path, and to remove any directory information from the uploaded file-name.

You'll also want to avoid sending the complete details of any exception to the client. This can often expose internal information which can be used by hackers to attack your site. The built-in ASP.NET error handlers do a pretty good job of hiding this information from remote users by default.
 
Share this answer
 
Please try below code:

Looks like dropdownlist value is not properly fetched in query.
C#
string checkuser = "select count(*) from tbl_member where pre_name='" +  ddPreN.SelectedItem.Text+ "'";

Note: If you want to fetch dropdown selected value then use ddPreN.SelectedItem.Value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900