Reputation: 1807
I quite new to c# and asp.net so plz tell me if i give you to little info for my question.
When a user is logging in I create an instance of my User object, calling my getcategories method and the redirect then user to another page. Like this:
if (dt.Rows.Count > 0){
user apa = new user();
apa.namn = dt.Rows[0]["FirstName"].ToString() + " " + dt.Rows[0]["LastName"].ToString();
apa.mail = dt.Rows[0]["Email"].ToString();
apa.id = dt.Rows[0]["ID"].ToString();
apa.firstname = dt.Rows[0]["FirstName"].ToString();
apa.lastnamn = dt.Rows[0]["LastName"].ToString();
apa.password = dt.Rows[0]["Password"].ToString();
Session["user"] = apa;
apa.getcategories();
Response.Redirect("visainlagg.aspx");
}
The problem is that I get "object reference not set to an instance of an object"-error on "kategorier[i].Name = dt.Rows[i]["Name"].ToString();" (the first thing that happens in my for loop in User class). I do not understand why :(
This is how the User class looks like:
public string namn;
public string mail;
public string id;
public string firstname;
public string lastname;
public string password;
public string constr = "secret";
public Post[] poster;
public anvcateg[] kategorier;
public int antalKategorier;
public void getcategories() {
SqlConnection conn = new SqlConnection();
conn.ConnectionString = constr;
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.CommandText = "SELECT * FROM Category WHERE Author= '" + id + "'";
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = com;
DataTable dt = new DataTable();
adp.Fill(dt);
antalKategorier = dt.Rows.Count;
kategorier = new anvcateg[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
kategorier[i].Name = dt.Rows[i]["Name"].ToString();
kategorier[i].ID = dt.Rows[i]["ID"].ToString();
kategorier[i].Description = dt.Rows[i]["Description"].ToString();
kategorier[i].Author = dt.Rows[i]["Author"].ToString();
}
}
the anvcateg class that getcategories() is using looks like this:
public class anvcateg
{
public string ID;
public string Name;
public string Description;
public string Author;
Upvotes: 0
Views: 1605
Reputation: 1063559
Your array contains all nulls - you have not created any objects. You have created an empty array. A quick fix would be:
kategorier[i] = new anvcateg();
at the start of each iteration.
For info, your current code is risky (SQL injection), doesn't clean up after itself, and unnecessarily complicated. Here's the same via "dapper":
using(var conn = new SqlConnection(constr)) {
conn.Open();
var data = conn.Query<anvcateg>(
"SELECT * FROM Category WHERE Author=@id",
new { id }).ToList();
}
(or ToArray() if you prefer)
This does all the mapping internally, cleans up the connection, and safely parameterises the "id" avoiding injection attacks. And no DataTable to be seen.
Upvotes: 1
Reputation: 12351
kategorier is an array of anvcateg
You initialized the array here:
kategorier = new anvcateg[dt.Rows.Count];
But you have to create an instance of anvcateg to add to your array)
for (int i = 0; i < dt.Rows.Count; i++)
{
kategorier[i] = new anvcateg()
{
ID = dt.Rows[i]["ID"].ToString(),
Author = dt.Rows[i]["Author"].ToString(),
Description = dt.Rows[i]["Description"].ToString(),
Name = dt.Rows[i]["Name"].ToString()
};
}
Upvotes: 2
Reputation: 22793
Your question is not quite clear, try to find the code "kategorier[i].Name = dt.Rows[i]["Name"].ToString();" but could not found it. if you can post your code which contain these lines then it would be easy to answer you.
Anyway, there may be the following reason of this error:
Either dt.Rows[i]["Name"] is null and you are trying to convert it into string by using .ToString(), change this to Convert.ToString(dt.Rows[i]["Name"])
check you have defined "kategorier" as an array or you can use the List kategorier = new List kategoriers();
Otherwise your code looks fine
Upvotes: 0