开发者

C# homepage related question. (Request.Querystring["id"])

开发者 https://www.devze.com 2023-02-11 00:36 出处:网络
i am currently working on a homepage for a guild that will come up and running in rift in the near future, thus i am trying to finish the homepage as fast as possible.

i am currently working on a homepage for a guild that will come up and running in rift in the near future, thus i am trying to finish the homepage as fast as possible.

Anyway! i usually program homepages in VB.NET and i program c# when i program applications. but to become better at c# i choose to program the homepage in c#.

So heres my problem, i am currently trying to make a foreach that pulls out all the database stuff related to the id in the addressbar, so if my address bar says "ShowTopics.aspx?id=1" then everything in the table in the database that has the number 1 (i use a field called fldovertopicid so when someone adds a new under topic to the overtopic then it gets the same number as the overtopics id.) gets pulled out. though the problem is that it开发者_如何学JAVA seems like it doesent work as easily as the vb.net programming does.

so heres my code:

in this code i give the method a name so that when i write that name later in my code it knows that, thats what its supposed to do. as you can see ive put a 'Where fldOverTopicID=" + id;' so i tell the code that i want to use fldOverTopicID as a mediator so that the code knows that when the addressbar number matches fldOverTopicID then its supposed to pull out everything with the same number.

public DataTable GetUnderTopics(int id)
    {
        string strsql = "select fldid, fldtopicname, fldicon, fldcreator from tbltopics where fldOverTopicId=" + id;
        MySqlCommand objCMD = new MySqlCommand(strsql);

        return _objdata.GetData(objCMD, _con);
    }

This is the backend code for where i want to pull out the topics. what im trying to tell it to do here is that if the number in the addressbar is 1 then its supposed to pull out all content that have a 1 in its fldOverTopicID. but this is were i get my problem. when i made that piece of code i got 2 errors:

  1. The best overloaded method match for 'Forumfac.GetUnderTopics(int)' has some invalid arguments

  2. Argument 1: cannot convert from 'string' to 'int'

        Forumfac objfac = new Forumfac();
    
        foreach (DataRow row in objfac.GetUnderTopics(Request.QueryString["id"]).Rows)
        {
        }
    

Hopefully i was able to explain my problem clearly enough.

Thanks in advance


Try this

string idFromQueryString = Request.QueryString["id"];

if (! string.IsNullOrEmpty( idFromQueryString))
{
  int id = Convert.ToInt32(idFromQueryString );
  Forumfac objfac = new Forumfac();

  foreach (DataRow row in objfac.GetUnderTopics(id).Rows)
  {
  }
}


use objFac.GetUnderTopics(int.Parse(Request.Querystring["id"]))

You are making an invalid call to the method which takes an int as an argument. All querystring parameters will be strings. int.Parse(string) converts the value from a string to an integer or throws an exception if the string is not an integer. There is also a TryParse(string, out int) method which returns whether or not the string could be converted and does not throw an exception.


I voted for StackOverflowException's answer because it should work. But if I were writing this I'd use TryParse like this...

int id;
if (int.TryParse(Request.QueryString["id"], out id))
{
  Forumfac objfac = new Forumfac();

  foreach (DataRow row in objfac.GetUnderTopics(id).Rows)
  {
  }
}

It's one less step and it'll guarantee reliable results if the id can't be parsed.

0

精彩评论

暂无评论...
验证码 换一张
取 消