Click here to Skip to main content
15,609,550 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.Configuration;
using System.IO;

public partial class addsize : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
                string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/Database/registration.accdb");
                using (OleDbConnection con = new OleDbConnection(ConnectionString))
                    OleDbDataAdapter da = new OleDbDataAdapter(" SELECT * FROM tblcategory", con);
                    DataSet ds = new DataSet();
                    DropDownList1.DataSource = ds.Tables[0];
                    DropDownList1.DataTextField = "catname";
                    DropDownList1.DataValueField = "catid";
                    DropDownList1.Items.Insert(0, new ListItem("--Select--", "0"));
    protected void Button1_Click(object sender, EventArgs e)
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/Database/registration.accdb");

            OleDbDataAdapter SQLAdapter = new OleDbDataAdapter("insert into size([sizename]) values('" + TextBox1.Text + "' )", con);
            DataTable DT = new DataTable();
            Response.Write("Size Added Successfully");
            TextBox1.Text = string.Empty;
            TextBox1.Text = "";
            DropDownList1.Items.FindByValue("0").Selected = true;

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        int maincategoryid = Convert.ToInt32(DropDownList1.SelectedItem.Value);
        string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/Database/registration.accdb");
        using (OleDbConnection con = new OleDbConnection(ConnectionString))
            OleDbDataAdapter ds = new OleDbDataAdapter(" SELECT * FROM subcategory where maincatid='"+DropDownList1.SelectedItem.Value+"'", con);
            DataSet dt = new DataSet();
            ds.Fill(dt);//error is here
            DropDownList2.DataSource = dt.Tables[0];
            DropDownList2.DataTextField = "subcatname";
            DropDownList2.DataValueField = "subcatid";
            DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));

What I have tried:

Error message: Data type mismatch in criteria expression
Updated 16-Aug-21 0:52am
PIEBALDconsult 16-Aug-21 20:12pm    
I'm sure it also tells you which line.

1 solution

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that through your whole app, validate your user inputs so that numeric values are passed as numbers rather than strings, and your problem should go away at the same time.
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