Please help!
I have a program that uses a stored procedure to retrieve data from the database and dumps the data in a table for display, this is how my code looks:
string connectionString = ConfigurationManager.ConnectionStrings["azcom"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
cmd = new SqlCommand("search_person", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@searchString", SqlDbType.VarChar).Value = searchString;
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
DataTable table = new DataTable();
while (reader.Read())
{
table.Columns.Add("IDNumber", typeof(string));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Surname", typeof(string));
table.Columns.Add("Company Name", typeof(string));
table.Rows.Add("@idnumber", "@name", "@surname", "@companyN");
}
table.Load(reader);
lblDisplay.Text = table.ToString(); }
The problem is my SqlDataReader returns a null which causes my program to give me this error:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
Anyone who has an idea what might be the cause please help me.
SqlDataAdapter.Fill(table). Btw, what shouldtable.ToString()display? ADataTableis a complex object with no customToString.DataTableand its columns once before enumerating the reader - and then you need to actually go get the values from the reader by usingstring name = reader.GetString(1)and so on ...DataColumnsin the loop? You should do that once before you execute the reader.