How to insert values in one column? c# [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have the following code:
foreach (string value in data1)
{
var match = Regex.Match(value, #"(?<Number>\d+)(?<Text>.*)");
var number = match.Groups["Number"].Value;
var text = match.Groups["Text"].Value;
string result2 = string.Format("{0}", text);
data.Rows.Add(result2);
dataGridView1.DataSource = data;
}
This code will add the data to my first column. How do i fix it to make that data to be added to the second column instead?

I would think you need move the line of binding datasource out of the loop block firstly,
foreach (string value in data1)
{
var match = Regex.Match(value, #"(?<Number>\d+)(?<Text>.*)");
var number = match.Groups["Number"].Value;
var text = match.Groups["Text"].Value;
string result2 = string.Format("{0}", text);
data.Rows.Add(result2);
}
dataGridView1.DataSource = data;

DataColumn Col = datatable.Columns.Add("Column Name", typeof(Boolean));
Col.SetOrdinal(0);

DataRow newRow = data.NewRow();
newRow["Text"] = text; //if the column name is Text
data.Rows.Add(newRow);

Related

Error when converting a synchronous method to an asynchronous method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 days ago.
Improve this question
I am trying to convert this block of code to Async
public static List<Employee> EmployeesList()
{
List<Employee> list = new List<Employee>();
DataTable dt = new DataTable();
dt = Data.DataAccess.ExecuteDataTable("[Employee].[dbo].[Employee_Select]");
foreach (DataRow row in dt.Rows)
{
list.Add(new Employee()
{
Id = Convert.ToInt32(row["ID"]),
Name = Convert.ToString(row["Name"]),
Email = Convert.ToString(row["Email"]),
Age = Convert.ToInt32(row["Age"]),
Salary = Convert.ToInt32(row["Salary"])
});
}
return list;
}
The modified code looks like this
public static async Task<List<Employee>> EmployeesList()
{
List<Employee> list = new List<Employee>();
DataTable dt = new DataTable();
dt = await Data.DataAccess.ExecuteDataTableAsync("[Employee].[dbo].[Employee_Select]");
foreach (DataRow row in dt.Rows)
{
list.Add(new Employee()
{
Id = Convert.ToInt32(row["ID"]),
Name = Convert.ToString(row["Name"]),
Email = Convert.ToString(row["Email"]),
Age = Convert.ToInt32(row["Age"]),
Salary = Convert.ToInt32(row["Salary"])
});
}
return list;
}
Edit for clarity. The synchronous method returns a list. The asynchronous method does not (It just exits the block and skipping the break points under the await), which causes a null reference error further down the path. Why isn't my Asynchronous method returning any results?
Ignore this part from the original post.
The problem I am having is that my async code block seems to be exiting before the data is returned because the Razor page gives me an Object reference not set to an instance of an object error on my Model.Employees. What am I doing wrong?
What is your calling code to this method?
Once you enter into async\await, you need to await all the calling methods.
You need to await EmployeeList(). Otherwise the calling method prematurely given control and your list is always null.
Hope this helps

Compare DB Item with an List<string> Input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
This is the data from my input
{Test,Sample}
This is the data from my database
{C:\Users\Test,D:\DriveB\Sample\Test,C:\Users\Private\Item\LocationB}
My expected output is
{C:\Users\Test,D:\DriveB\Sample\Test}
How can I get results out using that input? So far I have tried using a for loop like this
var Count = Input.Count;
for(var i = 0; i < Count; i++)
{
data = data.Where(u => u.Location.Contains(Input[i])).ToList();
}
The variable data has already been extracted from the database and has a format like this
Id
Name
Location
FileType
But the problem is that when it goes past the first loop, it immediately eliminates the second inputs data.
var Count = Input.Count;
data = data.Where(x => input.Any(y => x.Location.Contains(y))).ToList();
}
would likely work. The use of Any means:
Get me any row from data where at least one of the entries from input is contained in (i.e. a substring of) the Location.
Use split by "," first
var input = new List<String>() { "Test", "Sample" };
var db = #"C:\Users\Test,D:\DriveB\Sample\Test,C:\Users\Private\Item\LocationB";
var result = db.Split(',').Where(p => input.Any(c => p.Contains(c))).ToList();

Fill a ComboBox [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I need fill a ComboBox (Windows Forms) where have a Text and ID values. Example:
("Team1", 15)
("Team2", 27)
...
My code didnt work :/
List<Team> teams = new List<Team>();
teams = sq.loadTeams();
foreach (Team t in teams){
Combobox.Items.Add(t.getName(), t.getId());
}
heeelp please
Use data-binding, best feature Windows Forms come up with ;)
var list = new[]
{
new Team { Id = 1, Name = "One" },
new Team { Id = 2, Name = "Two" },
new Team { Id = 3, Name = "Three" }
};
combobox.ValueMember = "Id"; // Name of property to represent a Value
combobox.DisplayMember = "Name"; // Name of property to represent displayed text.
combobox.DataSource = list; // Bind all items to the control
Selections can be accessed by Selected.. properties of combobox.
var selectedTeamId = (int)combobox.SelectedValue;
var selectedTeamName = combobox.SelectedText;
var selectedTeam = (Team)combobox.SelectedItem;
Notice that SelectedValue and SelectedItem return object type, so you need cast it to correct type before using.

How to remove null values elements from model list in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I try to Extract data using LINQ query and adding into a model list.In this list only I need some of the fields values, but it's returning all the table fields include with null values. I have use model first approach so all the model classes will create the get set methods with all the table fields name so I want to remove some fields when I am retrieving the data Is it possible??
This my code
for (var z = 0; z < room_ament_list.Count(); z++)
{
var deal_room_amentity_id = room_ament_list[z];
var deal_room_amentity_details = db.deal_room_amentity.Where(a => a.room_amenity_id == deal_room_amentity_id).ToList();
foreach (var item_r_amentity in deal_room_amentity_details)
{
deal_room_amentity_list.Add(new deal_room_amentity()
{
room_amenity_id = item_r_amentity.room_amenity_id,
amenity_type = item_r_amentity.amenity_type,
});
}
}
this my table structure
And this is returning data from that list
But I need to Extract the data of what I have assigned in the list(without null elements).Is it possible to remove those null value element from the list??
If I am understanding your question correctly, you could modify you LINQ query as follows:
var deal_room_amentity_details = db.deal_room_amentity
.Where(a => a.room_amenity_id == deal_room_amentity_id
&& a.room_amenity_id != null).ToList();
You can download just the information you need from a SQL table by specifying an object definition in your Linq query.
var data = from x in db.deal_room_amentity
where
x.room_amenity_id == deal_room_amentity_id
select new {
id = x.id,
room_amenity_id = x.room_amenity_id
};
foreach(var item_r_amentity in data){
deal_room_amentity_list.Add(new deal_room_amentity()
{
room_amenity_id = item_r_amentity.room_amenity_id,
amenity_type = item_r_amentity.amenity_type,
});
}

ListItem not working with winforms [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I just simply trying to add items with values to a combobox from a XML file at runtime in a
windows application. The code below does not work in windows development.
//Code:
XDocument xDoc = XDocument.Load(#"Yourxmlfile.xml");
var query = from xEle in xDoc.Descendants("publication")
select new ListItem(xEle.Element("name").Value,
xEle.Attribute("tcmid").Value);
cmbLoad.ValueMember = "value";
cmbLoad.DisplayMember = "text";
cmbLoad.DataSource = query;
In the above code the ListItem class is not available for winforms so i couldn't proceed.
The above code works fine with web application.
Any help?
You can achieve that using this code:
XDocument xDoc = XDocument.Load(#"Yourxmlfile.xml");
var query = from xEle in xDoc.Descendants("publication")
select new { value = xEle.Element("name").Value, text = xEle.Attribute("tcmid").Value };
var list = query.ToList();
comboBox1.ValueMember = "value";
comboBox1.DisplayMember = "text";
comboBox1.DataSource = list;
ListItem doesn't exist for WinForms. I used an anonymous class above and converted it to a list which I can use as a source for a combo box.

Categories