Populate ComboBox control with data from database [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am doing a c# WFA program. I want the comboBox to be able to display the car brand from the data grid view but I'm not sure how to do that.
See Image

You want it from a database? Then you could try something like this.
string Sql = "select brand from [Car]";
SqlConnection conn = new SqlConnection(#"path_to_db");
conn.Open();
SqlCommand cmd = new SqlCommand(Sql, conn);
SqlDataReader DR = cmd.ExecuteReader();
while (DR.Read())
{
Invoke(new Action(() => ComboBox1.Items.Add(DR[0])));
}

Related

Can someone explain me how i can set the value that i would like to write inside a textbox/combo box/date timepicker? [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 1 year ago.
Improve this question
This is the first time when I am working with databases managerial systems, beside what I have studied at the University. So, my question for you guys is this one: how can i write inside an add button function the dataAdapter.InsertCommand.Parameters.Add for every one of the boxex that i mentioned above and also what should i write after equal for combo boxex and date time picker?
dataAdapter.InsertCommand = new SqlCommand("INSERT INTO Materiale(mNumar, mDenumire, mUM, mGreutate, mData) VALUES (#mNumar, #mDenumire, #mUM, #mGreutate, #mData", connectionString);
dataAdapter.InsertCommand.Parameters.Add("#mNumar", SqlDbType.Int).Value = numarTextBox.Text; ;
dataAdapter.InsertCommand.Parameters.Add("#mDenumire", SqlDbType.VarChar).Value = denumireTextBox.Text;
dataAdapter.InsertCommand.Parameters.Add("#mUM", SqlDbType.VarChar).Value = unitateMasuraComboBox.SelectedItem;
dataAdapter.InsertCommand.Parameters.Add("#mGreutate", SqlDbType.Int).Value = greutateNumericUpDowm.Value;
dataAdapter.InsertCommand.Parameters.Add("#mData", SqlDbType.Date).Value = DateTimePickerFormat.Custom.ToString("yyyy-mm-dd");
This is the code and based on this code I am asking the question :D
I will thank you from the beggining :D
As your reaction in a comment explains:
"I'm not getting an actual error, am getting an exception, like this "Incorect syntax near "#mData" "
An exception is an error. Which mostly leads to the solution.
The incorrect syntax is because you forgot to close the VALUES parentheses.
dataAdapter.InsertCommand = new SqlCommand(
"INSERT INTO Materiale(mNumar, mDenumire, mUM, mGreutate, mData)
VALUES (#mNumar, #mDenumire, #mUM, #mGreutate, #mData", connectionString);
^
#mData", connectionString); should be #mData)", connectionString);

How to access a SQL database in visual studio? (Razor cshtml, C#) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I Already connected my SQL database with visual studio via server explorer And i filled some tables with values.
Now, I want to access/search through a table from my SQL database in my visual studio project.
For example: in my table i have products from different categories and i only want to display products from a specific category
But, how can in do that?
There is many ways by which you can access.If you are using connected mode Than you have to create object for connection string
SqlConnection con = new SqlConnection(#"Data Source=nameof_server;Initial Catalog=name_of_database;Integrated Security=True");
SqlCommand cmd = new SqlCommand("sql_command",con);
con.open()
//perform action based on your requirment
ExecuteNonQuery() // Use this for insert,update,delete
ExecuteScalar() // Use this for select single column
ExecuteReader() // Use this for select multiple records
con.close()
Please check this link

how do I validate a user input in a Textbox against a column from a SQL Server table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I did my homework prior to asking this questions. Though, none of the results that google showed functioned .
I have a textbox whose input I wish to validate against a list of values which exist in a column of a table (ASP.NET with C# and SQL Server 2014 Express) . Should user enter some other value, than the error must be displayed.
I have done multiple tryouts with CustomValidator control and one when an event on the Textbox (.TextChanged). But I lost something, may be in the details. Could you give me a practical solution and at best, guide towards a useful online resource to study the connection to databases from asp.net (c#)?
I am aware that I did not catch the subject.
Here is a basic rough version how you could get it done. Can also use a Stored procedure rather than direct..Wasn't sure if you meant after a button was clicked or not, but you can put this in a method and have it return true or false if its valid.
SqlConnection cnn = null;
SqlCommand cmd = null;
SqlDataAdapter sda = null;
DataTable Dt = new Datatable();
cnn = new SqlConnection(strConnectionString);
cmd = new SqlCommand("Select COLUMN FROM WHEREVER WHERE VALUE =#TextboxValue", cnn);
cnn.Open();
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("#TextboxValue", SqlDbType.VarChar).Value = Textbox.Text;
sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.rows.count > 0 )
{
//MATCH FOUND
}

C# MySQL DataRow with multiple values [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
Is there a way to do a c# datarow like the below code in a SELECT * query
//Have results like blow
string username = (string)row["username"];
I've tried but all I seem to see is reader or something, witch I know nothing about and don't understand. Can you lead me to some code that will help or give me a example?
DataReader is actually exactly what you need. The 'DataRow' class by itself won't help you; that gets used as part of a more complex solution, the 'DataSet' class (which uses 'DataTable' and that in turn uses 'DataColumn' and 'DataRow'). I don't see many people using 'DataSet'; if you want something complex with drag-and-drop design, you should look at using Entity Framework.
Here is a standard way to read values from SQL in .NET via DataReader (which, no matter what anyone says, is the fastest way to simply read data from a SQL database in .NET):
using (var connection = new SqlConnection("<Your connection string here>")
{
var command = new SqlCommand(
"SELECT username, email FROM users;",
connection);
connection.Open();
var reader = command.ExecuteReader(); // Using the DataReader (specifically, the SqlDataReader)
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("User {0} has email {1}", reader["username"],
reader["email"]);
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
MSDN documentation for DataReader

SQL Server and Visual Studio 2012 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have connected to my database using Data Connection Server Explorer and the same is added to my web.config as well. Now can someone tell me how to query it?
Sure, you could do this:
using (SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["key_of_element"]))
{
c.Open();
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Table"))
{
var reader = cmd.ExecuteReader();
while (reader.Read())
{
// do something with the data
}
}
}

Categories