No row at position 0. Need to check if row count > 0 before filling table and going to next query
I'm posting the upper portion of code. No matter what I do the compiler says I'm missing a name space or I'm using a method like a class or its expecting 'or ='. I'm totally new to C# programming.
The problem: Query checks if people are in your network. People with others in network, no error. People without others in their network, error. (I didnt even get to looping through the network query to populate the 'friends' section in the XML portion if friends are in their network)
Thanks is advance for the help
<%@ Page language="c#" %>
<script runat="server" language="C#">
protected void Page_Load(object sender, EventArgs e)
{
string xml;
try
{
xml = Auth(Request.Params["uid"].ToString());
}
catch (Exception ex)
{
xml = ex.Message;
}
if (!string.IsNullOrEmpty(xml))
{
Response.Clear();
Response.ExpiresAbsolute = DateTime.Now;
Response.AddHeader("Content-type", "text/xml");
Response.Write(xml);
}
if (!string.IsNullOrEmpty(xml) || Request.Params["uid"] != string.Empty)
{
Response.End();
}
}
protected virtual string Auth(string uid)
{
String xml;
if (!String.IsNullOrEmpty(Request["uid"]))
{
System.Data.DataTable dtTable = new System.Data.DataTable();
System.Data.DataTable memTable = new System.Data.DataTable();
System.Data.DataTable netTable = new System.Data.DataTable();
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
conn.Open();
string query = "SELECT userid, username, city, state, country, birthday, img1, userLevelName, gendername, title FROM UserInfo INNER JOIN Gend ON UserInfo.gendID = Gend.gendID INNER JOIN UserLevel ON UserInfo.UserLevelID = UserLevel.UserLevelID WHERE userid='" + Request["uid"] + "'";
System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter(query, conn);
adp.Fill(dtTable);
//start of problem is that the next query checks to see if someone is in your network, people with others in network, no error. people without others in their network error
string query1 = "SELECT MemID FROM network WHERE userid='" + Request["uid"] + "'";
System.Data.SqlClient.SqlDataAdapter memdp = new System.开发者_JAVA技巧Data.SqlClient.SqlDataAdapter(query1, conn);
memdp.Fill(memTable);
string query2 = "SELECT Distinct userid, username, img1, birthday, gendid, city, state, country, title FROM UserInfo WHERE (userinfo.userid IN (" + memTable.Rows[0]["memid"] + ")) AND userinfo.img1 is not null order by userinfo.username";
System.Data.SqlClient.SqlDataAdapter netdp = new System.Data.SqlClient.SqlDataAdapter(query2, conn);
netdp.Fill(netTable);
//end of problem
conn.Close();
xml = string.Format("<login result=\"OK\">" +
"<userData>" +
"<id><![CDATA[" + dtTable.Rows[0]["userid"] + "]]></id>" +
"<name><![CDATA[" + dtTable.Rows[0]["username"] + "]]></name>" +
"<gender><![CDATA[" + dtTable.Rows[0]["gendername"] + "]]></gender>" +
"<location><![CDATA[" + dtTable.Rows[0]["city"] + "]]>, <![CDATA[" + dtTable.Rows[0]["state"] + "]]>, <![CDATA[" + dtTable.Rows[0]["country"] + "]]></location>" +
"<age>22</age>" +
"<photo>http://www.somesite.com/thumbnail.asp?path=" + dtTable.Rows[0]["img1"] + "</photo>" +
"<thumbnail>http://www.somesite.com/thumbnail.asp?path=" + dtTable.Rows[0]["img1"] + "</thumbnail>" +
"<details><![CDATA[" + dtTable.Rows[0]["title"] + "]]></details>" +
"<level><![CDATA[" + dtTable.Rows[0]["userlevelname"] + "]]></level>" +
"<profileUrl>http://www.somesite.com/" + dtTable.Rows[0]["username"] + "</profileUrl>" +
"</userData>" +
"<friends>" +
"<friend>" +
"<id>2</id>" +
"<name>Bill</name>" +
"<gender>0</gender>" +
"<location>London, UK</location>" +
"<age>22</age>" +
"<photo>http://yourdomain/photos/bill.png</photo>" +
"<thumbnail>http://yourdomain/photos/bill_small.png</thumbnail>" +
"<details>Hello, I am Bill</details>" +
"<level>regular</level>" +
"</friend>" +
"</friends>" +
You first query result is empty, it means that memTable has no rows and you trying to access first row when creating second query.
memdp.Fill(memTable);
if(memTable.Rows.Count != 0) //check if table has any rows
{
string query2 = ...//do other stuff
}
Try this.
protected string Auth(string uid)
{
string xml = String.Empty;
if (!String.IsNullOrEmpty(Request["uid"]))
{
string query1 = "SELECT userid, username, city, state, country, birthday, img1, userLevelName, gendername, title FROM UserInfo INNER JOIN Gend ON UserInfo.gendID = Gend.gendID INNER JOIN UserLevel ON UserInfo.UserLevelID = UserLevel.UserLevelID WHERE userid='" + Request["uid"] + "'";
System.Data.DataTable dtTable = FillDataTableFromDB(query1);
string query2 = "SELECT MemID FROM network WHERE userid='" + Request["uid"] + "'";
System.Data.DataTable memTable = FillDataTableFromDB(query2);
xml = "<login result=\"OK\">" +
"<userData>" +
"<id><![CDATA[" + dtTable.Rows[0]["userid"] + "]]></id>" +
"<name><![CDATA[" + dtTable.Rows[0]["username"] + "]]></name>" +
"<gender><![CDATA[" + dtTable.Rows[0]["gendername"] + "]]></gender>" +
"<location><![CDATA[" + dtTable.Rows[0]["city"] + "]]>, <![CDATA[" + dtTable.Rows[0]["state"] + "]]>, <![CDATA[" + dtTable.Rows[0]["country"] + "]]></location>" +
"<age>22</age>" +
"<photo>http://www.somesite.com/thumbnail.asp?path=" + dtTable.Rows[0]["img1"] + "</photo>" +
"<thumbnail>http://www.somesite.com/thumbnail.asp?path=" + dtTable.Rows[0]["img1"] + "</thumbnail>" +
"<details><![CDATA[" + dtTable.Rows[0]["title"] + "]]></details>" +
"<level><![CDATA[" + dtTable.Rows[0]["userlevelname"] + "]]></level>" +
"<profileUrl>http://www.somesite.com/" + dtTable.Rows[0]["username"] + "</profileUrl>" +
"</userData>";
if(memTable != null && memTable.Rows.Count >0)
{
string query3 = "SELECT Distinct userid, username, img1, birthday, gendid, city, state, country, title FROM UserInfo WHERE (userinfo.userid IN (" + memTable.Rows[0]["memid"] + ")) AND userinfo.img1 is not null order by userinfo.username";
System.Data.DataTable netTable = FillDataTableFromDB(query3);
if(netTable != null && netTable.Rows.Count >0)
{
xml = "<friends>";
foreach(System.Data.DataRow row in netTable.Rows)
{
xml = "<friend>" +
"<id><![CDATA[" + row["userid"] + "]]></id>" +
"<name>Bill</name>" +
"<gender>0</gender>" +
"<location>London, UK</location>" +
"<age>22</age>" +
"<photo>http://yourdomain/photos/bill.png</photo>" +
"<thumbnail>http://yourdomain/photos/bill_small.png</thumbnail>" +
"<details>Hello, I am Bill</details>" +
"<level>regular</level>" +
"</friend>";
}
xml = "</friends>";
}
}
}
return xml;
}
private System.Data.DataTable FillDataTableFromDB(string query)
{
System.Data.DataTable datatable = new System.Data.DataTable();
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString))
{
using (System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter(query, conn))
{
adapter.Fill(datatable);
}
}
return datatable;
}
P.S: Please do not code like what is given in your snippet. Functions
are there for a reason. Also there is something called StringBuilder
. Whats the point on using virtual
on aspx? This code just solves the errors. I am sad that you are not making a serious attempt at coding.
My first recomendation would be to put this code in a .aspx.cs file (codebehind). Youare not specifying a namesmace in the script file are there also any using statements youare missing?
ie:
using System;
namespace mynamespace
{
public class MyTestClass : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
I hope that helps. Could you post the exact error that you are recieving?
精彩评论