VB.NET combobox selected value connected to a SQL statement..?
-
now, i have some comboboxes, i want to insert a record to my database, wherein it will search the database from the value inside the combobox, then the record will add beside it... for example, (remember the comboboxes are fixed, and you will only input texts to the textbox and it will be recorded in the database triggered by a button) Company Name(combobox - for instance, i selected "Dell") Product (combobox - i selected "Laptops") Product Model (combobox - i selected "Inspiron") Enter your Product code: (Textbox - i put "GT911") after i entered a value from the textbox and clicked the button, how can it be done like this? CompanyName----Product------ProductMod… ProductCode(these line are fields) Dell--------------------Laptop--------… (it must be located here) Dell--------------------Desktop------X… Dell---------------------Printer------… how can i code these in sql statement?? i want the code to search the fields from the comboboxes and put the value of the textbox exactly where the comboboxes are lined.. please help me.. ill give 10 points for the best answer.. needed asap..
-
Answer:
Use SQL command with parameters (as an example I’ve used OleDb provider): Dim conn_str As String = 'enter your connection string here Dim conn As New OleDb.OleDbConnection(conn_str) Dim comm_str As String = "Update MyTable SET ProductCode = code WHERE CompanyName = comp AND Product = prod AND ProductModel = mod" Dim comm As New OleDb.OleDbCommand(comm_str, conn) 'assign values to parameters: comm.Parameters.AddWithValue("code", txtProductCode.Text) comm.Parameters.AddWithValue("comp", cboCompanyName.Text) comm.Parameters.AddWithValue("prod", cboProduct.Text) comm.Parameters.AddWithValue("mod", cboProductModel.Text) Try conn.Open() comm.ExecuteNonQuery() Catch ex As Exception MessageBox.Show(ex.Message) Finally conn.Close() End Try There are other ways to assign values to parameters, but main idea stays the same. Another way – instead of embedded SQL (shown above), you can create parameterized stored procedure.
TheNiceG... at Yahoo! Answers Visit the source
Other answers
I think Petro was mostly on track, except his code would change every existing Dell Inspiron laptop to have the supplied product code. Instead, since you said you were doing an insert instead of an update, just make the following change: Dim comm_str As String = "Insert Into MyTable (ProductCode, CompanyName, Product, ProductModel) Values (code, comp, prod, mod)"
TheMadProfessor
Related Q & A:
- How to save from DatagridView to Database VB.NET?Best solution by Stack Overflow
- How to properly end a javascript statement?Best solution by w3schools.com
- How to check if an IP is currently connected to a server?Best solution by serverfault.com
- How to write SQL statement with dynamic where clause?Best solution by Stack Overflow
- Why can't I connect my laptop to a printer that is connected to a desktop pc?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.