Avatar of aspnet-scotland
aspnet-scotland
Flag for United Kingdom of Great Britain and Northern Ireland asked on

How do I reference my database column within a url string in C#?

Hi,

Within the iteminserting event of my listview control I am attempting to redirect the user to a web page that is being pre-populated by a query string variable (customer_id) from my backend db.

I am using the attached C# code on my calling page but I get the error:

"System.Web.Query.Dynamic.ParseException: Operator '=' incompatible with operand types 'Int32' and 'Object"???

Any help appreciated.

Thanks.
protected void lvTrustAccounts_ItemInserting(object sender, ListViewInsertEventArgs e) 
    { 
        var customerID = e.Values["customer_id"]; 
        //Convert.ToInt32(customerID); 
         
        Response.Redirect("http://www.tradeselector.co.uk/customerTrades.aspx?trustid=" + customerID + ""); 
    }

Open in new window

.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
aspnet-scotland

8/22/2022 - Mon
Gary Davis

The var customerID is probably an object that should be a string (use e.Values["customer_id"].ToString()).
You should also UrlEncode the querystring parameter for safety (Server.UrlEncode(customerID)).
Gary Davis
Kaushal Arora

You are using
var customerID = e.Values["customer_id"];

in this the e.Values["customer_id"] is of object type and in customerID field you want ant Int32 type value so write the statement as var customerID = Convert.ToInt32(e.Values["customer_id"].ToString());
ASKER CERTIFIED SOLUTION
rajeeshmca

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
robasta

rajeeshmca is correct.

In addition, you may need to convert the string to an Int32 type on the receving page (assuming cutomer_id is an int32).
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
Gary Davis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
aspnet-scotland

ASKER
gardavis,

Don't you mean...

Response.Redirect("http://www.tradeselector.co.uk/customerTrades.aspx?trustid=Server.UrlEncode(" + customerID + ")");

Your suggestion doesn't seem to be able to pick up Server.UrlEncode??

Thanks.
aspnet-scotland

ASKER
actually ignore my previous question, I was being stupid....thanks!