c# - Why do I get a 'System.ArgumentOutOfRangeException' when setting the Text for a row cell? -


using number of queries, shown in code below, following exception:

an exception of type 'system.argumentoutofrangeexception' - specified argument out of range of valid values

on line:

e.row.cells[3].text = count; 

what problem? tried countless different things, can't working. novice @ this.

sqlconnection conn; conn = new sqlconnection("data source=.\\sqlexpress;attachdbfilename=" + server.mappath("~\\app_data\\forumdb.mdf") + ";integrated security=true;user instance=true"); conn.open(); sqlcommand comm; comm = new sqlcommand("select count(threadid) [threads] [topicid] = @topicid", conn); sqlcommand comm2; comm2 = new sqlcommand("select max(posteddate) [threads] [topicid] = @topicid", conn); sqlcommand comm3; comm3 = new sqlcommand("select postedby threads posteddate=(select max(posteddate) [threads] [topicid] = @topicid", conn);  //for command1 cmd comm.parameters.add("@topicid", system.data.sqldbtype.int, 10, "topicid"); comm.parameters["@topicid"].value = e.row.cells[0].text; string count = (comm.executescalar().tostring()); e.row.cells[3].text = count;  //for command2 cmd1 comm2.parameters.add("@topicid", system.data.sqldbtype.int, 10, "topicid"); comm2.parameters["@topicid"].value = e.row.cells[0].text; string count1 = (comm2.executescalar().tostring()); e.row.cells[4].text = count1;  //for command3 cmd2 comm3.parameters.add("@topicid", system.data.sqldbtype.int, 10, "topicid"); comm3.parameters["@topicid"].value = e.row.cells[0].text; if (comm3.executescalar() != null) {     count2 = (comm3.executescalar().tostring()); } conn.close(); 

the problem line:

e.row.cells[3].text = count; 

appears row not have 4 cells. check putting breakpoint on line, , looking @ value of e.row.cells.count. smaller 4.

also, may want change how performing queries, because seems can fields need in single query, instead of 3 separate ones:

const string query =      "select postedby, posteddate, count(threadid) count " +     "from [threads] [topicid] = @topicid " +     "order posteddate desc " +     "limit 1";  var filetoattach = server.mappath("~\\app_data\\forumdb.mdf"); var connectionstring = "data source=.\\sqlexpress;attachdbfilename=" + filetoattach + ";integrated security=true;user instance=true";  var topicid = int.parse(e.row.cells[0].text); var postedby = "";  using (var conn = new sqlconnection(connectionstring)) {     conn.open();     using (var cmd = new sqlcommand(query, conn)     {         cmd.parameters.addwithvalue("@topicid", topicid);         using (var reader = cmd.executereader())         {              if (reader.read())             {                 // note: assuming have these cells now!                 e.row.cells[3].text = reader["count"].tostring();                 e.row.cells[4].text = reader["posteddate"].tostring();                 postedby = (string)reader["postedby"];             }         }     } } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -