The database may be doing a case sensitive search. However, I believe the problem is that you are using "ExecuteNonQuery" which does not return any results when you should be using for example "ExecuteScalar".
Redirect URL from database
-
02-06-2022 - |
Question
I am doing a URL shortening service. But currently I am facing a problem. When I tried to enter the URL, for example:
http:/localhost:4021/Book/article
It is supposed to redirect me to the long URL like for example:
But when I enter the shortened URL, it couldn't redirect.
The following is my code. What is wrong?
string query;
string name = Page.RouteData.Values["Name"] as string;
string str = "Data Source=blankblankblank;Initial Catalog=URLSHORT;User ID=blank;Password=blank";
SqlConnection con = new SqlConnection(str);
con.Open();
query = "SELECT @long_url FROM dbo.url_map WHERE @short_url='" + name + "'";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("short_url", SqlDbType.VarChar).Value = tbShort.Text;
cmd.Parameters.Add("long_url", SqlDbType.VarChar).Value = tbLong.Text;
cmd.CommandText = string.Format("SELECT @long_url FROM dbo.url_map WHERE @short_url='" + name + "'");
if(name != null)
{
else if (name == cmd.ExecuteNonQuery().ToString())
{
Response.StatusCode = 303;
Response.Status = "303 Moved Permanently";
Response.RedirectLocation = "http://www.youtube.com";
Response.End();
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow