I have single DataSet.xsd file and in this i have two DataTable one is Bill and another is BillInfo now, i am fill writing this code
string qry = "Select * from BillInfo where [BillNumber]='" +txtbill.Text + "' and [Session]='"+CLS.ses+"'";
OleDbDataAdapter da = bm.returnDataAdaptor(qry);
DataSet ds1 = new DataSet();
da.Fill(ds1);
DataSets.Bill ds = new DataSets.Bill();
da.Fill(ds);
DataTable Dt1 = ds1.Tables[0];
DataTable dt = ds.Tables.Add("BillInfo");
dt.Columns.Add("BillNumber", Type.GetType("System.String"));
dt.Columns.Add("CustomerName", Type.GetType("System.String"));
dt.Columns.Add("City", Type.GetType("System.String"));
dt.Columns.Add("Mobile", Type.GetType("System.String"));
dt.Columns.Add("TotalAmount", Type.GetType("System.String"));
dt.Columns.Add("Packing", Type.GetType("System.String"));
dt.Columns.Add("Tax", Type.GetType("System.String"));
dt.Columns.Add("Transport", Type.GetType("System.String"));
dt.Columns.Add("Additional", Type.GetType("System.String"));
dt.Columns.Add("PaybleAmount", Type.GetType("System.Int32"));
dt.Columns.Add("Session", Type.GetType("System.String"));
dt.Columns.Add("Date1", Type.GetType("System.DateTime"));
dt.Columns.Add("Naration", Type.GetType("System.String"));
dt.Columns.Add("AmountWord", Type.GetType("System.String"));
dt.Columns.Add("Status", Type.GetType("System.String"));
dt.Columns.Add("Balance", Type.GetType("System.Int32"));
for (int i = 0; i < Dt1.Rows.Count; i++)
{
DataRow r = dt.NewRow();
r["BillNumber"] = Dt1.Rows[i][0];
r["CustomerName"] = Dt1.Rows[i][1];
r["City"] = Dt1.Rows[i][2];
r["Mobile"] = Dt1.Rows[i][3];
r["TotalAmount"] = Dt1.Rows[i][4];
r["Packing"] = Dt1.Rows[i][5];
r["Tax"] = Dt1.Rows[i][6];
r["Transport"] = Dt1.Rows[i][7];
r["Additional"] = Dt1.Rows[i][8];
r["PaybleAmount"] = Dt1.Rows[i][9];
r["Session"] = Dt1.Rows[i][10];
r["Date1"] = Dt1.Rows[i][11];
r["Naration"] = Dt1.Rows[i][12];
r["AmountWord"] = Dt1.Rows[i][13];
r["Status"] = Dt1.Rows[i][14];
r["Balance"] = Dt1.Rows[i][15];
dt.Rows.Add(r);
}
string qry1 = "Select * from BillDetails where [BillNumber]='" + txtbill.Text + "'";
OleDbDataAdapter da1 = bm.returnDataAdaptor(qry1);
DataSet ds2 = new DataSet();
da.Fill(ds2);
da.Fill(ds);
DataTable Dt2 = ds2.Tables[0];
DataTable dt2 = ds.Tables.Add("Bill");
dt2.Columns.Add("BillNumber", Type.GetType("System.String"));
dt2.Columns.Add("ProductCode", Type.GetType("System.String"));
dt2.Columns.Add("ProductName", Type.GetType("System.String"));
dt2.Columns.Add("productSize", Type.GetType("System.String"));
dt2.Columns.Add("ProductQuantity", Type.GetType("System.String"));
dt2.Columns.Add("ProductWeight", Type.GetType("System.String"));
dt2.Columns.Add("Unit", Type.GetType("System.String"));
dt2.Columns.Add("ProductPrice", Type.GetType("System.String"));
dt2.Columns.Add("Amount", Type.GetType("System.String"));
dt2.Columns.Add("Date1", Type.GetType("System.DateTime"));
for (int i = 0; i < Dt2.Rows.Count; i++)
{
DataRow r = dt.NewRow();
r["BillNumber"] = Dt2.Rows[i][0];
r["ProductCode"] = Dt2.Rows[i][1];
r["ProductName"] = Dt2.Rows[i][2];
r["productSize"] = Dt2.Rows[i][3];
r["ProductQuantity"] = Dt2.Rows[i][4];
r["ProductWeight"] = Dt2.Rows[i][5];
r["Unit"] = Dt2.Rows[i][6];
r["ProductPrice"] = Dt2.Rows[i][7];
r["Amount"] = Dt2.Rows[i][8];
r["Date1"] = Dt2.Rows[i][9];
dt2.Rows.Add(r);
}
but at the r["ProductCode"] = Dt2.Rows[i][1]; it is showing an exception. That is Column 'ProductCode' does not belong to table BillInfo. Please solve my error.
Replace
DataRow r = dt.NewRow();
with
DataRow r = dt2.NewRow();
You have simply used the wrong table which has no column ProductCode. It must be "Bill" instead of "BillInfo".
Apart from that:
You are open for sql-injection when you use textboxes as input for your sql query. Use parameters instead.
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparameter.aspx
The answer is very clear Column 'ProductCode' does not belong to table BillInfo instead I can see Column 'ProductCode' is in BillDetails.
Related
I have tried to create a data table in C#, but I want to achieve similar results to these shown in this screenshot:
I can create dynamic columns but can not create rows.
DataTable dt_bq = new DataTable();
dt_bq.Columns.Add("Date", typeof(string));
dt_bq.Columns.Add("12/8", typeof(string));
dt_bq.Columns.Add("13/8", typeof(string));
dt_bq.Columns.Add("Total", typeof(string));
DataRow dr_tot = dt_bq.NewRow();
https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable?view=netframework-4.8
// Setting up header of table
DataTable dt_bq = new DataTable();
dt_bq.Columns.Add("Date", typeof(string));
dt_bq.Columns.Add("12/8", typeof(string));
dt_bq.Columns.Add("13/8", typeof(string));
dt_bq.Columns.Add("14/8", typeof(string));
dt_bq.Columns.Add("Total", typeof(string));
// Insert rows for that table
DataRow dr_tot = dt_bq.NewRow();
dr_tot["Date"] = "Issue Qty";
dr_tot["12/8"] = "10";
dr_tot["13/8"] = "20";
dr_tot["14/8"] = "30";
dr_tot["Total"] = "60";
dt_bq.Rows.Add(dr_tot);
dr_tot = dt_bq.NewRow();
dr_tot["Date"] = "Retrun Qty";
dr_tot["12/8"] = "5";
dr_tot["13/8"] = "20";
dr_tot["14/8"] = "30";
dr_tot["Total"] = "55";
dt_bq.Rows.Add(dr_tot);
dr_tot = dt_bq.NewRow();
dr_tot["Date"] = "Balance";
dr_tot["12/8"] = "5";
dr_tot["13/8"] = "0";
dr_tot["14/8"] = "0";
dr_tot["Total"] = "5";
dt_bq.Rows.Add(dr_tot);
You can add rows with column's name as index to the row. Then insert that row to the table.
I want to bind student register number as in DataTable1 in Gridview1 with positions as in DataTable2 .
But, Gridview1 first page only bind, second page onwards not binding. I give my .net code , DB table structure and Gridview1 display below. If anybody give me solution, I will thankful to you.
C#.Net Code
private void dgvHallSeating()
{
DataSet ds = new DataSet();
DataTable dt;
DataTable dtSeatPositionsAssign = new DataTable();
DataColumn col1;
DataColumn col2;
DataColumn col3;
DataColumn col4;
DataColumn col5;
dt = new DataTable();
col1 = new DataColumn("col1", Type.GetType("System.String"));
col2 = new DataColumn("col2", Type.GetType("System.String"));
col3 = new DataColumn("col3", Type.GetType("System.String"));
col4 = new DataColumn("col4", Type.GetType("System.String"));
col5 = new DataColumn("col5", Type.GetType("System.String"));
dtSeatPositionsAssign.Columns.Add(col1);
dtSeatPositionsAssign.Columns.Add(col2);
dtSeatPositionsAssign.Columns.Add(col3);
dtSeatPositionsAssign.Columns.Add(col4);
dtSeatPositionsAssign.Columns.Add(col5);
Int32 SeatPositionColIndex = 0;
Int32 SeatPositionRowIndex = 0;
DataTable DataTable2 = new DataTable();
DataTable2 = CMSBL.dtecSeatSlot1Positions();
DataTable DataTable1 = CMSBL.rptecSeatSlot1Reports();
int RegNoRowIndex = 0;
int HallSeatPosition = 0;
for (; RegNoRowIndex < 11; RegNoRowIndex++)
{
for (; HallSeatPosition < DataTable2.Rows.Count; )
{
SeatPositionColIndex = Convert.ToInt32(DataTable2.Rows[HallSeatPosition]["ecGridColumnIndex"]);
SeatPositionColIndex = SeatPositionColIndex - 1;
SeatPositionRowIndex = Convert.ToInt32(DataTable2.Rows[HallSeatPosition]["ecGridRowIndex"]);
SeatPositionRowIndex = SeatPositionRowIndex - 1;
dtSeatPositionsAssign.Rows.Add();
if (SeatPositionRowIndex >= dtSeatPositionsAssign.Rows.Count || GridView1.Rows.Count <= 5)
{
dtSeatPositionsAssign.Rows.Add();
}
dtSeatPositionsAssign.Rows[SeatPositionRowIndex][SeatPositionColIndex] = DataTable1.Rows[RegNoRowIndex][3].ToString();
break;
}
HallSeatPosition++;
}
GridView1.DataSource = dtSeatPositionsAssign;
GridView1.DataBind();
}
**
DataTable1
**
Datatable2
**
Gridview1 Display
I have two datatables dt1 and dt2 ,where as dt1 contains of a single column "ID" and dt2 contains of a single column "NAME". My requirement is to combine both the datatables and create a new datatable (ie)
whereas as mentioned above i need to get the datas of both the datatable (dt1 & dt2 ) and need to create a new datatable.
You can do something like this
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("id", typeof(Int32));
dt2.Columns.Add("Name", typeof(String));
DataRow dr = dt1.NewRow();
dr["id"] = 1;
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr["id"] = 2;
dt1.Rows.Add(dr);
dr = dt2.NewRow();
dr["name"] = "XXX";
dt2.Rows.Add(dr);
dr = dt2.NewRow();
dr["name"] = "YYY";
dt2.Rows.Add(dr);
DataTable dt3 = new DataTable();
dt3.Columns.Add("id", typeof(Int32));
dt3.Columns.Add("Name", typeof(String));
for (int i = 0; i < dt1.Rows.Count; i++)
{
dr = dt3.NewRow();
dr["id"] = dt1.Rows[i]["id"];
dr["name"] = dt2.Rows[i]["name"];
dt3.Rows.Add(dr);
}
IF you want to combine to DataTables, you can do the following:
DataTable table1 = new DataTable("Items");
DataColumn column1 = new DataColumn("id", typeof(System.Int32));
table1.Columns.Add(column1);
DataTable table2 = new DataTable("details");
DataColumn column = new DataColumn("Name", typeof(System.String));
table1.Columns.Add(column);
table1.Merge(table2); //table1 will have 2 columns after executing this line
I have 2 datasets. I want to fetch a row from first dataset and insert it into another dataset.
I have Created Two DataSet with name ds1 and ds2.
Than i have created 4 columns and 3 ROWS and added to ds1.
Now In ds2
I have created again 4 columns with same column names in ds2.
Now we have to add row from ds1 to ds2.
So for example that i have copied row (with index 2) from ds1 and adding to ds2.
Thanking you and ask if you have any further query.
DataSet ds1 = new DataSet();
// Adding new 4 column to ds1
ds1.Tables.Add(new DataTable());
ds1.Tables[0].Columns.Add("column_1", typeof(string));
ds1.Tables[0].Columns.Add("column_2", typeof(string));
ds1.Tables[0].Columns.Add("column_3", typeof(string));
ds1.Tables[0].Columns.Add("column_4", typeof(string));
DataRow row_1 = ds1.Tables[0].NewRow();
row_1["column_1"] = "row1cell1";
row_1["column_2"] = "row1cell2";
row_1["column_3"] = "row1cell3";
row_1["column_4"] = "row1cell4";
ds1.Tables[0].Rows.Add(row_1);
DataRow row_2 = ds1.Tables[0].NewRow();
row_2["column_1"] = "row2cell1";
row_2["column_2"] = "row2cell2";
row_2["column_3"] = "row2cell3";
row_2["column_4"] = "row2cell4";
ds1.Tables[0].Rows.Add(row_2);
DataRow row_3 = ds1.Tables[0].NewRow();
row_3["column_1"] = "row3cell1";
row_3["column_2"] = "row3cell2";
row_3["column_3"] = "row3cell3";
row_3["column_4"] = "row3cell4";
ds1.Tables[0].Rows.Add(row_3);
GridView1.DataSource = ds1;
GridView1.DataBind();
DataSet ds2 = new DataSet();
ds2.Tables.Add(new DataTable());
ds2.Tables[0].Columns.Add("column_1", typeof(string));
ds2.Tables[0].Columns.Add("column_2", typeof(string));
ds2.Tables[0].Columns.Add("column_3", typeof(string));
ds2.Tables[0].Columns.Add("column_4", typeof(string));
DataRow ds2row_1 = ds2.Tables[0].NewRow();
ds2row_1["column_1"] = ds1.Tables[0].Rows[2]["column_1"].ToString();
ds2row_1["column_2"] = ds1.Tables[0].Rows[2]["column_2"].ToString();
ds2row_1["column_3"] = ds1.Tables[0].Rows[2]["column_3"].ToString();
ds2row_1["column_4"] = ds1.Tables[0].Rows[2]["column_4"].ToString();
ds2.Tables[0].Rows.Add(ds2row_1);
GridView2.DataSource = ds2;
GridView2.DataBind();
i tried to bind a datagrid but there is a problem in binding of my datagrid..
C# code
DataSet ds = new DataSet();
DataTable dt = ds.Tables.Add("Source");
dt.Columns.Add("ID", Type.GetType("System.String"));
dt.Columns.Add("Desc", Type.GetType("System.String"));
Insurance oInsurance = new Insurance();
List<Value> lstValue = oInsurance.Category.ValueList;
foreach (Value item in lstValue)
{
DataRow dr = dt.NewRow();
dr[0] = item.Key.ToString();
dr[1] = item.Value.ToString();
dt.Rows.Add(dr);
}
grdCategory.DataSource = ds;
grdCategory.DataMember = "Source";
grdCategory.DataTextField = "Desc";
grdCategory.DataValueField = "ID";
grdCategory.DataBind();
Thanks
well... try to post the error that you are getting from this...
but... if you don't need the DataSet you could just do like this...
Insurance oInsurance = new Insurance();
List<Value> lstValue = oInsurance.Category.ValueList;
grdCategory.DataSource = lstValue;
grdCategory.AutoGenerateColumns = true; //not sure that's the property
grdCategory.DataBind();