1 Replies - 1136 Views - Last Post: 28 September 2011 - 03:09 PM Rate Topic: -----

#1 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

problem with CAST() function in ASP.NET

Posted 28 September 2011 - 10:12 AM

HI ALL.I Have this method for building a string and i want to use that string in RowFilter of a defaultview.
private String FilteriStringProduktet()
        {
            String filteri = "";
            if (ddlKategoria1.SelectedIndex != -1)
            {
                if (filteri != "")
                {
                    filteri += " AND Kategoria LIKE '" + ddlKategoria1.SelectedValue.Trim() + "%'";
                }
                else
                {
                    filteri += "Kategoria LIKE '" + ddlKategoria1.SelectedValue.Trim() + "%'";
                }

            }
            if (ddlProdhuesi.SelectedIndex != -1)
            {
                if (filteri != "")
                {
                    filteri += " AND Prodhuesi LIKE '" + ddlProdhuesi.SelectedValue.Trim() + "%'";
                }
                else
                {
                    filteri += "Prodhuesi LIKE '" + ddlProdhuesi.SelectedValue.Trim() + "%'";
                }
            }
            if (ddlSasiaVogel.SelectedIndex != -1)
            {
                if (filteri != "" && ddlSasiaVogel.SelectedValue == "%")
                {
                    filteri += " AND (CAST(Sasia AS Varchar(50))) LIKE '" + ddlSasiaVogel.SelectedValue.Trim() + "%'";
                }
                else if(filteri == "" && ddlSasiaVogel.SelectedValue == "%")
                {
                    filteri += "(CAST(Sasia AS Varchar(50))) LIKE  = '" + ddlSasiaVogel.SelectedValue.Trim() + "%'";
                }
                else if (filteri != "" && ddlSasiaVogel.SelectedValue != "%")
                {
                    filteri += " AND Sasia < " + ddlSasiaVogel.SelectedItem.Text.Trim() + "";
                }
                else if (filteri == "" && ddlSasiaVogel.SelectedValue != "%")
                {
                    filteri += " Sasia < " + ddlSasiaVogel.SelectedItem.Text.Trim() + "";
                }
                
            }
            if (ddlSasiaMadhe.SelectedIndex != -1)
            {
                if (filteri != "" && ddlSasiaMadhe.SelectedValue == "%")
                {
                    filteri += " AND (CAST(Sasia AS Varchar(50))) LIKE '" + ddlSasiaMadhe.SelectedValue.Trim() + "%'";
                }
                else if (filteri == "" && ddlSasiaMadhe.SelectedValue == "%")
                {
                    filteri += "(CAST(Sasia AS Varchar(50))) LIKE '" + ddlSasiaMadhe.SelectedValue.Trim() + "%'";
                }
                else if (filteri != "" && ddlSasiaMadhe.SelectedValue != "%")
                {
                    filteri += " AND Sasia > " + ddlSasiaMadhe.SelectedItem.Text.Trim() + "";
                }
                else if (filteri == "" && ddlSasiaMadhe.SelectedValue != "%")
                {
                    filteri += " Sasia > " + ddlSasiaMadhe.SelectedItem.Text.Trim() + "";
                }
            }
            if (ddlCmimiVogel.SelectedIndex != -1)
            {
                if (filteri != "" && ddlCmimiVogel.SelectedValue == "%")
                {
                    filteri += " AND (CAST(Cmimi AS Varchar(50))) LIKE '" + ddlCmimiVogel.SelectedValue.Trim() + "%'";
                }
                else if (filteri == "" && ddlCmimiVogel.SelectedValue == "%")
                {
                    filteri += "(CAST(Cmimi AS Varchar(50))) '" + ddlCmimiVogel.SelectedValue.Trim() + "%'";
                }
                else if (filteri != "" && ddlCmimiVogel.SelectedValue != "%")
                {
                    filteri += " AND Cmimi < " + ddlCmimiVogel.SelectedItem.Text.Trim() + "";
                }
                else if (filteri == "" && ddlCmimiVogel.SelectedValue != "%")
                {
                    filteri += "Cmimi < " + ddlCmimiVogel.SelectedItem.Text.Trim() + "";
                }
            }
            if (ddlCmimiMadh.SelectedIndex != -1)
            {
                if (filteri != "" && ddlCmimiMadh.SelectedValue == "%")
                {
                    filteri += " AND (CAST(Cmimi AS Varchar(50))) LIKE '" + ddlCmimiMadh.SelectedValue.Trim() + "%'";
                }
                else if (filteri == "" && ddlCmimiMadh.SelectedValue == "%")
                {
                    filteri += "(CAST(Cmimi AS Varchar(50))) LIKE '" + ddlCmimiMadh.SelectedValue.Trim() + "%'";
                }
                else if (filteri != "" && ddlCmimiMadh.SelectedValue != "%")
                {
                    filteri += " AND Cmimi > " + ddlCmimiMadh.SelectedItem.Text.Trim() + "";
                }
                else if (filteri == "" && ddlCmimiMadh.SelectedValue != "%")
                {
                    filteri += "Cmimi > " + ddlCmimiMadh.SelectedItem.Text.Trim() + "";
                }
            }



I have 6 DropDownLists to filter my grid view but when i try filtering it says: "The expression contains undefined function call CAST()"

I Tried replacing CAST with CONVERT but than it says : "Syntax error: Missing operand after 'AS' operator."


In MS SQL it works with Cast() but not in asp.net.I thin there is any mystake with string given to RowFIlter.


And this is one of my dropdowlists textChanged event:

protected void ddlKategoria1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String filter = FilteriStringProduktet().Trim();
            DataTable dt = Session["tblProduktet"] as DataTable;
            if (dt != null)
            {
                dt.DefaultView.RowFilter = filter;
            }

            GVProduktet.DataSource = Session["tblProduktet"];
            GVProduktet.DataBind();
        }



This si String filter built for RowFilter:

"Kategoria LIKE 'HDD%' AND Prodhuesi LIKE '%%' AND (CAST(Sasia AS Varchar(50))) LIKE '%%' AND (CAST(Sasia AS Varchar(50))) LIKE '%%' AND (CAST(Cmimi AS Varchar(50))) LIKE '%%' AND (CAST(Cmimi AS Varchar(50))) LIKE '%%'"



Sasia is a column of type INT
Cmimi is a comulmn of type DECIMAL

This post has been edited by drilli: 28 September 2011 - 10:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem with CAST() function in ASP.NET

#2 drilli  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 105
  • Joined: 26-May 09

Re: problem with CAST() function in ASP.NET

Posted 28 September 2011 - 03:09 PM

Well I Found where was the problem :

Replace CAST with CONVERT(columnName,System.String) because RowFilter works with .NET and datatype in .NET is String not varchar!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1