I have a datatable from a stored procedure. I want to add a column named "Assessment" which will be populated from a sql query. Finally I want to populate a store with this datatable. In visual studio I see that the datatable column that contains the "SiteId" is named 7. But I receive this error:
Load failed:Column '7' does not belong to table .
and the code:
DAL.DBDataContext dc = new DAL.DBDataContext();
try
{
SqlConnection sqlConnection1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader sqlreader;
cmd.CommandText = "StorProc";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("#CTid", ctid));
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
sqlreader = cmd.ExecuteReader();
var dt = new DataTable();
dt.Load(sqlreader);
//Add assessment column
dt.Columns.Add("Assessment", typeof(System.String));
var data = new List<object>();
foreach (DataRow r in dt.Rows)
{
if (r[7] != null)
{
var res = (from d in dc.Doctors
join ct in dc.CTUsers on d.id equals ct.DoctorId
where ct.CTid == ctid && ct.SiteId == Int32.Parse(r["7"].ToString())
select d).FirstOrDefault();
r["Assessment"] = res.Assessment;
}
data.Add(r);
}
this.storeSites.DataSource = dt;
this.storeSites.DataBind();
sqlConnection1.Close();
}
catch (Exception a)
{
X.Msg.Alert("Warning", "Load failed:" + a.Message).Show();
}
Please try this:
foreach (DataRow r in dt.Rows)
{
foreach(DataColumn column in dt.Columns)
{
if (r[column] != null && r[column].ColumnName == "some column name")
{
var res = (from d in dc.Doctors
join ct in dc.CTUsers on d.id equals ct.DoctorId
where ct.CTid == ctid && ct.SiteId == Int32.Parse(r[column].ToString())
select d).FirstOrDefault();
r["Assessment"] = res.Assessment;
}
}
data.Add(r);
}
Int32.Parse(r["7"].ToString() is not correct. You are requesting a column named 7.
I think the above code is more clear.
Edit
You can also access the row-column value like this:
r.ItemArray[i].ToString()
Like so:
for (int i = 0; i < dt.Rows.Count; i++)
{
var r = dt.Rows[i];
var value = r.ItemArray[7].ToString();
if (!String.IsNullOrEmpty(value))
{
var res = (from d in dc.Doctors
join ct in dc.CTUsers on d.id equals ct.DoctorId
where ct.CTid == ctid && ct.SiteId == Int32.Parse(value)
select d).FirstOrDefault();
r["Assessment"] = res.Assessment;
}
data.Add(r);
}
The DataRow class has an overloaded indexer to access the columns.
public object this[int columnIndex] { get; set; }
... accesses the column by its int index (this is what we can see on your image). You use it by passing it an int:
object o = r[7]; // NOT r["7"] !
The other overload
public object this[string columnName] { get; set; }
... requires a column name passed as string:
object o = r["Assessment"];
If the column was really named "7", then you would have to access it like this:
object o = r["7"]; // But this is wrong in your case!
Note that the type of the argument passed to the index makes the difference. So r[7] and r["Assessment"] both work in your case. Using the name is preferred, as the index might change if the schema or the query changes. r["7"] does not work, as there is no column named like this.
See:
Member Overloading on MSDN.
Overloading, C# in Depth by Jon Skeet.
Related
I am trying to implement an ADO.NET code which executes the SQL query with multiple parameters. Looks like SQL parameter limit is 2100 and does not accept more than this limit. How do I achieve with my below code to have this accept more than the limitation.
I am finding it difficult to understand the implementations when validating online articles related how to send the queries in subsets or chunks to fulfill my request.
This is my code:
using (Connection = new SqlConnection(CS))
{
Connection.Open();
string query = "SELECT FamilyID, FullName, Alias FROM TABLE (nolock) WHERE FamilyID IN ({0})";
var stringBuiler = new StringBuilder();
var familyIds = new List<string>();
string line;
while ((line = TextFileReader.ReadLine()) != null)
{
line = line.Trim();
if (!familyIds.Contains(line) & !string.IsNullOrEmpty(line))
{
familyIds.Add(line);
}
}
var sqlCommand = new SqlCommand
{
Connection = Connection,
CommandType = CommandType.Text
};
var index = 0; // Reset the index
var idParameterList = new List<string>();
foreach (var familyId in familyIds)
{
var paramName = "#familyId" + index;
sqlCommand.Parameters.AddWithValue(paramName, familyId);
idParameterList.Add(paramName);
index++;
}
sqlCommand.CommandText = String.Format(query, string.Join(",", idParameterList));
var dt = new DataTable();
using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
{
dt.Load(sqlReader);
}
try
{
if (dt.Rows.Count > 0)
{
OutputdataGridView.DataSource = lstDownloadOwnerOutput;
OutputdataGridView.ColumnHeadersDefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
OutputdataGridView.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
Gridviewdisplaylabel.Text = "Total no of rows: " + this.OutputdataGridView.Rows.Count.ToString();
}
else if (dt.Rows.Count == 0)
{
MessageBox.Show("Data returned blank!!!");
}
}
catch (Exception Ex)
{
if (Connection != null)
{
Connection.Close();
}
MessageBox.Show(Ex.Message);
}
}
Having a WHERE IN clause with 2100, or even 100, parameters is generally not good coding practice. You might want to consider putting those values into a separate bona fide table, e.g.
families (ID int PK, ...)
Then, you may rewrite your query as:
SELECT FamilyID, FullName, Alias
FROM TABLE (nolock)
WHERE FamilyID IN (SELECT ID FROM families);
You could also express the above using an EXISTS clause or a join, but all three approaches might just optimize to a very similar query plan anyway.
You can just add a table load call every 2000 parameters in your code:
var index = 0; // Reset the index
var idParameterList = new List<string>();
var dt = new DataTable();
foreach (var familyId in familyIds) {
var paramName = "#familyId" + index;
sqlCommand.Parameters.AddWithValue(paramName, familyId);
idParameterList.Add(paramName);
index++;
if (index > 2000) {
sqlCommand.CommandText = String.Format(query, string.Join(",", idParameterList));
using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
dt.Load(sqlReader);
sqlCommand.Parameters.Clear();
idParameterList.Clear();
index = 0;
}
}
if (index > 0) {
sqlCommand.CommandText = String.Format(query, string.Join(",", idParameterList));
using (SqlDataReader sqlReader = sqlCommand.ExecuteReader())
dt.Load(sqlReader);
}
For dynamic sql like this, I generally recommend using a Table-Valued Parameter.
It does require a bit of setup: you have to create a user-defined Type in the DB to hold the values, but that is a fairly trivial operation:
CREATE TYPE PrimaryKeyType AS TABLE ( VALUE INT NOT NULL );
We generally use these in conjunction with stored procedures:
CREATE PROCEDURE dbo.getFamily(#PrimaryKeys PrimaryKeyType READONLY)
AS
SELECT FamilyID, FullName, Alias
FROM TABLE (nolock) INNER JOIN #PrimaryKeys ON TABLE.FamilyID = #PrimaryKeys.Value
GO
However, you can also use inline SQL if you prefer.
Assigning the values to the stored proc or inline parameter is fairly straightforward, but there is one gotcha (more later):
public static void AssignValuesToPKTableTypeParameter(DbParameter parameter, ICollection<int> primaryKeys)
{
// Exceptions are handled by the caller
var sqlParameter = parameter as SqlParameter;
if (sqlParameter != null && sqlParameter.SqlDbType == SqlDbType.Structured)
{
// The type name may look like DatabaseName.dbo.PrimaryKeyType,
// so remove the database name if it is present
var parts = sqlParameter.TypeName.Split('.');
if (parts.Length == 3)
{
sqlParameter.TypeName = parts[1] + "." + parts[2];
}
}
if (primaryKeys == null)
{
primaryKeys = new List<int>();
}
var table = new DataTable();
table.Columns.Add("Value", typeof(int));
foreach (var wPrimaryKey in primaryKeys)
{
table.Rows.Add(wPrimaryKey);
}
parameter.Value = table;
}
The thing to watch out for here is the naming of the parameter. See the code in the method above that removes the database name to resolve this issue.
If you have dynamic SQL, you can generate a correct parameter using the following method:
public static SqlParameter CreateTableValuedParameter(string typeName, string parameterName)
{
// Exceptions are handled by the caller
var oParameter = new SqlParameter();
oParameter.ParameterName = parameterName;
oParameter.SqlDbType = SqlDbType.Structured;
oParameter.TypeName = typeName;
return oParameter;
}
Where typeName is the name of your type in the DB.
I have two columns in a datatable:
ID, Calls.
How do I find what the value of Calls is where ID = 5?
5 could be anynumber, its just for example. Each row has a unique ID.
Make a string criteria to search for, like this:
string searchExpression = "ID = 5"
Then use the .Select() method of the DataTable object, like this:
DataRow[] foundRows = YourDataTable.Select(searchExpression);
Now you can loop through the results, like this:
int numberOfCalls;
bool result;
foreach(DataRow dr in foundRows)
{
// Get value of Calls here
result = Int32.TryParse(dr["Calls"], out numberOfCalls);
// Optionally, you can check the result of the attempted try parse here
// and do something if you wish
if(result)
{
// Try parse to 32-bit integer worked
}
else
{
// Try parse to 32-bit integer failed
}
}
You can use LINQ to DataSet/DataTable
var rows = dt.AsEnumerable()
.Where(r=> r.Field<int>("ID") == 5);
Since each row has a unique ID, you should use Single/SingleOrDefault which would throw exception if you get multiple records back.
DataRow dr = dt.AsEnumerable()
.SingleOrDefault(r=> r.Field<int>("ID") == 5);
(Substitute int for the type of your ID field)
I could use the following code.
Thanks everyone.
int intID = 5;
DataTable Dt = MyFuctions.GetData();
Dt.PrimaryKey = new DataColumn[] { Dt.Columns["ID"] };
DataRow Drw = Dt.Rows.Find(intID);
if (Drw != null) Dt.Rows.Remove(Drw);
You can try with method select
DataRow[] rows = table.Select("ID = 7");
DataRow dataRow = dataTable.AsEnumerable().FirstOrDefault(r => Convert.ToInt32(r["ID"]) == 5);
if (dataRow != null)
{
// code
}
If it is a typed DataSet:
MyDatasetType.MyDataTableRow dataRow = dataSet.MyDataTable.FirstOrDefault(r => r.ID == 5);
if (dataRow != null)
{
// code
}
try this code
DataRow foundRow = FinalDt.Rows.Find(Value);
but set at lease one primary key
Hello just create a simple function that looks as shown below.. That returns all rows where the call parameter entered is valid or true.
public DataTable SearchRecords(string Col1, DataTable RecordDT_, int KeyWORD)
{
TempTable = RecordDT_;
DataView DV = new DataView(TempTable);
DV.RowFilter = string.Format(string.Format("Convert({0},'System.String')",Col1) + " LIKE '{0}'", KeyWORD);
return DV.ToTable();
}
and simply call it as shown below;
DataTable RowsFound=SearchRecords("IdColumn", OriginalTable,5);
where 5 is the ID.
Thanks..
Try avoiding unnecessary loops and go for this if needed.
string SearchByColumn = "ColumnName=" + value;
DataRow[] hasRows = currentDataTable.Select(SearchByColumn);
if (hasRows.Length == 0)
{
//your logic goes here
}
else
{
//your logic goes here
}
If you want to search by specific ID then there should be a primary key in a table.
I have two columns in a datatable:
ID, Calls.
How do I find what the value of Calls is where ID = 5?
5 could be anynumber, its just for example. Each row has a unique ID.
Make a string criteria to search for, like this:
string searchExpression = "ID = 5"
Then use the .Select() method of the DataTable object, like this:
DataRow[] foundRows = YourDataTable.Select(searchExpression);
Now you can loop through the results, like this:
int numberOfCalls;
bool result;
foreach(DataRow dr in foundRows)
{
// Get value of Calls here
result = Int32.TryParse(dr["Calls"], out numberOfCalls);
// Optionally, you can check the result of the attempted try parse here
// and do something if you wish
if(result)
{
// Try parse to 32-bit integer worked
}
else
{
// Try parse to 32-bit integer failed
}
}
You can use LINQ to DataSet/DataTable
var rows = dt.AsEnumerable()
.Where(r=> r.Field<int>("ID") == 5);
Since each row has a unique ID, you should use Single/SingleOrDefault which would throw exception if you get multiple records back.
DataRow dr = dt.AsEnumerable()
.SingleOrDefault(r=> r.Field<int>("ID") == 5);
(Substitute int for the type of your ID field)
I could use the following code.
Thanks everyone.
int intID = 5;
DataTable Dt = MyFuctions.GetData();
Dt.PrimaryKey = new DataColumn[] { Dt.Columns["ID"] };
DataRow Drw = Dt.Rows.Find(intID);
if (Drw != null) Dt.Rows.Remove(Drw);
You can try with method select
DataRow[] rows = table.Select("ID = 7");
DataRow dataRow = dataTable.AsEnumerable().FirstOrDefault(r => Convert.ToInt32(r["ID"]) == 5);
if (dataRow != null)
{
// code
}
If it is a typed DataSet:
MyDatasetType.MyDataTableRow dataRow = dataSet.MyDataTable.FirstOrDefault(r => r.ID == 5);
if (dataRow != null)
{
// code
}
try this code
DataRow foundRow = FinalDt.Rows.Find(Value);
but set at lease one primary key
Hello just create a simple function that looks as shown below.. That returns all rows where the call parameter entered is valid or true.
public DataTable SearchRecords(string Col1, DataTable RecordDT_, int KeyWORD)
{
TempTable = RecordDT_;
DataView DV = new DataView(TempTable);
DV.RowFilter = string.Format(string.Format("Convert({0},'System.String')",Col1) + " LIKE '{0}'", KeyWORD);
return DV.ToTable();
}
and simply call it as shown below;
DataTable RowsFound=SearchRecords("IdColumn", OriginalTable,5);
where 5 is the ID.
Thanks..
Try avoiding unnecessary loops and go for this if needed.
string SearchByColumn = "ColumnName=" + value;
DataRow[] hasRows = currentDataTable.Select(SearchByColumn);
if (hasRows.Length == 0)
{
//your logic goes here
}
else
{
//your logic goes here
}
If you want to search by specific ID then there should be a primary key in a table.
By trying to avoid to many foreach()->if() routines, i tried a lambda-concept to search through a bunch of DataTables. I had no errors until i debugged my code to see it does not work, because its not allowed to ask my datarow-column for its index... is there any way to make this work instead of using IndexOf()?
static Entity.Produkt ProduktConstructor(DataRow dr)
{
Entity.Produkt p = new Entity.Produkt();
DataTable dt = Entity.KbMirror.mirror.Tables["Produkt"];
p.id = Guid.Parse(dr[0].ToString());
p.name = dr[1].ToString();
byte[] ba = dt.Rows[dt.IndexOf(dt.Select().Where(r => r[0].ToString() == p.id.ToString()))]["ProduktLogo"];
p.logo = Converter.ImageConverter.BA2Image(ba);
foreach (DataRow pvdr in Entity.KbMirror.mirror.Tables["Produkt_Version"].Rows)
if (Guid.Parse(pvdr[1].ToString()) == p.id)
p.version.Add(VersionConstructor(Guid.Parse(pvdr[2].ToString() ), Guid.Parse(pvdr[0].ToString() ) ) );
return p;
}
static Entity.Version VersionConstructor(Guid vid, Guid pvid)
{
Entity.Version version = new Entity.Version();
DataTable dt = Entity.KbMirror.mirror.Tables["Version"];
version.id = vid;
version.name = dt.Rows[dt.IndexOf(dt.Select().Where(r =>r[0].ToString() == vid.ToString()))][1].ToString();
foreach (DataRow cvdr in Entity.KbMirror.mirror.Tables["Customer_ProduktVersion"].Rows)
if (Guid.Parse(cvdr[2].ToString()) == pvid)
version.customerCollection.Add(CustomerConstructor(Guid.Parse(cvdr[1].ToString())));
return version;
}
EDIT:
error occures when I use "IndexOf()" like this:
byte[] ba = dt.Rows[dt.IndexOf(dt.Select().Where(r => r[0].ToString() == p.id.ToString()))]["ProduktLogo"];
Have you tried passing the entire table as opposed to just the row? Once you pass the entire table, then you can reference just that row of the table.
Also: http://msdn.microsoft.com/en-us/library/bb552415%28v=vs.110%29.aspx
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);
DataTable products = ds.Tables["Product"];
IEnumerable<DataRow> query =
from product in products.AsEnumerable()
select product;
Console.WriteLine("Product Names:");
foreach (DataRow p in query)
{
Console.WriteLine(p.Field<string>("Name"));
}
Sir I have filled my dataset with linq as
public void FillDataSet(DataSet ds1,int Id)
{
try
{
var y = from ins in cstmrDC.customers_rd(Id) select ins;
var z = from ins in cstmrDC.customersCntcts_rd(Id) select ins;
DataTable dtCst = new DataTable("dtCstmr");
dtCst.Columns.Add("cst_Id");
dtCst.Columns.Add("cst_Name");
dtCst.Columns.Add("cst_SName");
dtCst.Columns.Add("cst_AdLn1");
DataTable dtDtls = new DataTable("dtDtails");
dtDtls.Columns.Add("cst_SrlNo");
dtDtls.Columns.Add("cst_CntName");
dtDtls.Columns.Add("cst_cntDsgn");
foreach (var dtbl in y)
{
DataRow dr;
dr = dtCst.NewRow();
dr[0] = dtbl.cust_Id;
dr[1] = dtbl.cust_Name;
dr[2] = dtbl.cust_Sname;
dr[3] = dtbl.cust_Adrsln1;
dtCst.Rows.Add(dr);
}
foreach (var dtbl in z)
{
DataRow drDtls;
drDtls = dtDtls.NewRow();
drDtls[0] = dtbl.cust_Slno;
drDtls[1] = dtbl.cust_Cntctnm;
drDtls[2] = dtbl.cust_Cntctdesig;
dtDtls.Rows.Add(drDtls);
}
ds1.Tables.Add(dtCst);
ds1.Tables.Add(dtDtls);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
and the Id is passing from another class as
intId = int.Parse(txtSearch.Text);
cstCls.FillDataSet(ds1w, intId);
from that dataset iam fillimg my textbox controllers and giving theDataSource to the dataGridView as
dgvCustInfo.DataSource = ds1w.Tables["dtDtails"];
In this way if I searched 1st time with integer id 1055 meanse the exact result is comming from database. At the same time If I gave the another integer Id as 1066 meance Iam getting exception as DataTable named 'dtCstmr' already belongs to this DataSet .. Soo how can I solve the problem.
You can check if a table is already contained in a DataSet with Contains:
if(!ds1.Tables.Contains(dtCst.TableName))
ds1.Tables.Add(dtCst);
if(!ds1.Tables.Contains(dtDtls.TableName))
ds1.Tables.Add(dtDtls);
However, as Raphael has mentioned this would not refresh the table in the DataSet. So an easy way would be to remove the old table and add the new:
if(ds1.Tables.Contains(dtCst.TableName))
ds1.Tables.Remove(dtCst.TableName);
if(ds1.Tables.Contains(dtDtls.TableName))
ds1.Tables.Remove(dtDtls.TableName);
ds1.Tables.Add(dtCst);
ds1.Tables.Add(dtDtls);
It's quite a bad idea to create and populate in the same method.
Your code is really confusing.
Create another method :
public void CreateTables(DataSet ds1) {
var dtCst = new DataTable("dtCstmr");
dtCst.Columns.Add("cst_Id");
dtCst.Columns.Add("cst_Name");
dtCst.Columns.Add("cst_SName");
dtCst.Columns.Add("cst_AdLn1");
var dtDtls = new DataTable("dtDtails");
dtDtls.Columns.Add("cst_SrlNo");
dtDtls.Columns.Add("cst_CntName");
dtDtls.Columns.Add("cst_cntDsgn");
ds1.Tables.Add(dtCst);
ds1.Tables.Add(dtDtls);
}
public void FillDataSet(DataSet ds1,int Id)
{
try
{
var y = from ins in cstmrDC.customers_rd(Id) select ins;
var z = from ins in cstmrDC.customersCntcts_rd(Id) select ins;
var dtCst = ds1.Tables["dtCstmr"];
var dtDtls = ds1.Tables["dtDtails"];
dtCst.Clear();
dtDtls.Clear();
foreach (var dtbl in y)
{
DataRow dr;
dr = dtCst.NewRow();
dr[0] = dtbl.cust_Id;
dr[1] = dtbl.cust_Name;
dr[2] = dtbl.cust_Sname;
dr[3] = dtbl.cust_Adrsln1;
dtCst.Rows.Add(dr);
}
foreach (var dtbl in z)
{
DataRow drDtls;
drDtls = dtDtls.NewRow();
drDtls[0] = dtbl.cust_Slno;
drDtls[1] = dtbl.cust_Cntctnm;
drDtls[2] = dtbl.cust_Cntctdesig;
dtDtls.Rows.Add(drDtls);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Method CreateTables should be called only one time, and FillDataSet as many times as needed.