I have two columns from excel file:
delivery_date1 - delivery_date2
2013-10-14 - null
null - 2013-10-19
I want to update deliveries table using the two columns from the uploaded excel file but in the my table, I only have one column which is the delivery_date.
What I would like to have is like this (based on the excel data above):
delivery_date
2013-10-14
2013-10-19
How can I do that using asp.net mvc?
Here's my code:
delivery_id = Convert.ToInt32(DB.db.Insert("dbo.deliveries", "delivery_id", new {
delivery_date = delivery_date1,
delivery_date = delivery_date2,
}));
I got an error like this:
An anonymous type cannot have multiple properties with the same name.
Which part of that error do you not understand? You cannot use the same property name twice!
I think this is what you want...
new
{
delivery_date = delivery_date1 == null ? delivery_date2 : delivery_date1
}
This will create a single property called delivery_date and will assign it the value of delivery_date1, or if delivery_date1 is null then it will be assigned with the value of delivery_date2
Related
I want to read the the combobox column as shown in the picture, i've tried using
string mode = dt.Rows[0]["Mode"].ToString();
But it doesn't work, it read the USID column instead.
How to read the combobox value?
datatable
private void getmode()
{
//from here
var modelist = new List<string>() { "Reg0", "Basic", "Extended" };
dgvmode.DataSource = modelist;
dgvmode.HeaderText = "Mode";
dtDataGridView.Columns.Add(dgvmode);
// until here, i've create a combobox column in my datatable
string mode = dt.Rows[i]["Mode"].ToString(); // this line got error, it can't read the "Mode" column and say it is not exist, but in my picture, it is exist as a combobox column.
Messagebox.Show(mode); // here to show i get the combobox value
}
datatable is unable to reada datagridview feature, thus, when i using
yourdatatable.Rows[i]["Mode"].ToString();
it will comes out an error (something likie "Mode" column doesnt exist)
even you change the "Mode" to an integer that indicate the column you want to read, it will automatically read the column that create by datatable only. For example :
yourdatatable.Rows[i][0].ToString();
it wont read the combobox column and it will read normal column instead (will not read combobox column ("Mode"), which is the first column. But it will read the column ("USID"), which is the second column).
then, i need to use datagridview like the code below to read the combobox value:
yourDataGridView.Rows[i].Cells[0].Value.ToString()
I am trying to read Excel data and bulk upload the same into the respective tables in SQL database. In my Excel Worksheet, the name of worksheet represents the database table name in which the data needs to be inserted.
So in the below code in var "tablename" consists the worksheet name, and I need to pass the respective table model class in typeof(____), inorder to get the table column names, after which the table column name from the model will be compared with database table column names and the bulk upload operation will be performed.
So can anyone please help me to pass the value stored in tableName as a class in:
typeof(_______).GetProperties().Select(p => p.Name);
Code:
var tableName = employeeData.TableName;
var sourceColumnList = typeof(ConsentReportSheetTableInfo).GetProperties().Select(p => p.Name);
You might try
var sourceColumnList = Type.GetType(employeeData.TableName).GetProperties().Select(p => p.Name);
The simplest way is to use Assembly.GetType(name).
So, you could use something like:
var assembly = Assembly.GetExecutingAssembly();
var type = assembly.GetType(tableName);
var properties = type.GetProperties().Select(p => p.Name);
(This assumes that the type is in the executing assembly, of course.)
You can find the official documentation with examples here.
i have a small project in c#, in one of my forms i have a datagridview with 3 columns one of the columns is comboboxcolumn that bind with a sql server table id and name, i want to insert the datagridview values into another table in the comboboxcolumn i want to insert the id not a name, anyone can tel me how to do that please? this is my insert code
`
for (int i = 0; i < dgv_student.Rows.Count; i++)
{
sc.add_student(dgv_student.Rows[i].Cells[0].Value.ToString(),
Convert.ToInt32(dgv_student.Rows[i].Cells[1].Value),
dgv_student.Rows[i].Cells[2].Value.ToString());
}
`
the cell[1] the the combobox that display a name not the id.
In your example you have :
Name ID
X X
As your database set. When you are getting this from your database you're probably doing :
myDataGridView.DataSource = GetAllFromMyDatabase(); // Select * from stuff
myDataGridView.DataBind();
Giving you all three columns, and the value. Now, if you want to insert only one column, why not just filter the data you're getting from your database/gridview ?
myComboBox.DataSource = GetOnlyIDFromMyDatabaseOrMyDataGrid(); // Select ID from Stuff
myComboBox.DataBind();
If you're binding your asp.net control with only the data you need, rather than getting everything and then filter, you will have less overhead to think about.
I am not talking about the id of the element in the list, but the id of it from a table if the value is in another table. E.g I have a FullName and an ID in one table and I have the same ID and some other stuff in another table(one to zero or one relationship). I have bound the FullName to a dropdown list control,but when saving,I need to refer to it's ID from the table, not the string value.
If you place something in a drop down list, you can place the ID in the value field and something else as a text, for example :
ddlCategorie.DataTextField = "Texte";
ddlCategorie.DataValueField = "ID_GLOBAL";
ddlCategorie.DataSource = db.GLOBAL.Where(t => t.DATE_FIN > dt).OrderByDescending(t => t.ID_GLOBAL).ToList();
ddlCategorie.DataBind();
As you see, I already placed the reel database value "ID" of the object inside the value field of the drop down list. So I can immediately retrieve is ID by doing :
int i = Convert.ToInt32(ddlCategorie.SelectedValue);
I have used the following code to get one more than the biggest ID in a table using razor
#foreach (var top in db.Query("SELECT MAX(ID)+1 as ID FROM mytable"))
{
if (#top.ID == null) {#top.ID = 1; }
}
if the table is empty, #top.ID returns null. I am trying to set its value to 1 if it is null. It however shows an error in the assignment part. How do you assign the value of #top.ID to something else? Or is there a way to user the sql query to set ID as 1 if the table is empty?
You can use
select isnull(max(ID),1) from YourTable
However, if you're trying to generate a new ID for a new record, you'd be better advised to use an identity field.