Solved

Saving Selected Combobox Values to a SQL Server Table

Posted on 2012-03-20
8
258 Views
Last Modified: 2012-08-14
Hi There,

I have four combo boxes from where I select the values and these values I would like to store it in a table, plase let me know which is the best method to do this.

Thanks,

Farman
0
Comment
Question by:farooque
  • 5
  • 3
8 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
see here, good tutorial for beginners
http://www.codeproject.com/Articles/12381/Using-ADO-NET-programmatically-with-C

just replace select queries with insert queries as per your table structure....
0
 

Author Comment

by:farooque
Comment Utility
PT_Window_ID      PT_UD_Key      PT_UD_Number      STRGA255      DEX_ROW_ID
PRODDETAILS          023-000034-1100          1                             CORALINE                     11421

my PT_Window_ID is the window name
       PT_UD_Key is the main key where I will store the item number
       PT_UD_Number is the Window ID
       STRGA255 is the place where my Combobox value will go

So if you have an example for this, please let me know

Thanks,
0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
using(sqlconnection conn=new sqlconnection("connectionstring"))
{
sqlcommand cmd=new sqlCommand("insert into mytable(windowid,udkey,udnumber,strga)
values(@WindowId,@UDKey,@UDNumber,@Strga)",conn);

cmd.parameters.add(@WindowID,SqlDbType.int,cmbWindowId.selectedValue);
cmd.parameters.add(@UDKey,SqlDbType.int,cmbudId.selectedValue);
cmd.parameters.add(@UDNumber,SqlDbType.int,cmbudnumber.selectedValue);
cmd.parameters.add(@Strga,SqlDbType.int,cmbstrga.selectedValue);

conn.open();
int retValues = cmd.ExecuteNonQuery();
}

Open in new window

0
 

Author Comment

by:farooque
Comment Utility
Where am I going wrong, I am using VB.Net to code and here is the code

      Dim Department As String
   
        Department = ComboDept.SelectedValue.ToString
 
        Dim cmdSave As New SqlCommand
        cmdSave.Connection = Conn
        cmdSave.CommandText = "Insert Into EXT00101([PT_window_ID], [PT_UD_Key], [PT_UD_Number], [STRGA255])Values(PRODUCTCLASS, ITEMNMBR, 1, @Department)"

        cmdSave.Parameters.Add("ProductClass", SqlDbType.VarChar)  ' PRODUCTCLASS is my constant value where I have to insert in the table every time
          cmdSave.ExecuteNonQuery()
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:farooque
Comment Utility
Hi There,

This is the code I used to insert into the table and the last row that gets inserted in the table says this "System.Data.DataRowView"

Below is my code, please let me know what needs to be corrected

   Dim cmdSave As New SqlCommand
        cmdSave.Connection = Conn
        cmdSave.CommandText = ("insert into EXT00101([PT_window_ID],[PT_UD_Key],[PT_UD_Number],[STRGA255]) values ('" & "PRODUCTCLASS" & "','" & LblDispItem.Text.ToString & "','" & 1 & "','" & ComboDept.SelectedValue.ToString & "')")
        cmdSave.CommandText = ("insert into EXT00101([PT_window_ID],[PT_UD_Key],[PT_UD_Number],[STRGA255]) values ('" & "PRODUCTCLASS" & "','" & LblDispItem.Text.ToString & "','" & 2 & "','" & CmbSubDept.SelectedValue.ToString & "')")
        cmdSave.CommandText = ("insert into EXT00101([PT_window_ID],[PT_UD_Key],[PT_UD_Number],[STRGA255]) values ('" & "PRODUCTCLASS" & "','" & LblDispItem.Text.ToString & "','" & 3 & "','" & CmbClass.SelectedValue.ToString & "')")
        cmdSave.CommandText = ("insert into EXT00101([PT_window_ID],[PT_UD_Key],[PT_UD_Number],[STRGA255]) values ('" & "PRODUCTCLASS" & "','" & LblDispItem.Text.ToString & "','" & 4 & "','" & CmbSubClass.SelectedValue.ToString & "')")

        cmdSave.ExecuteNonQuery()
0
 
LVL 20

Expert Comment

by:BuggyCoder
Comment Utility
Dim cmdSave As New SqlCommand
        cmdSave.Connection = Conn
        cmdSave.CommandText = ("insert into EXT00101([PT_window_ID],[PT_UD_Key],[PT_UD_Number],[STRGA255]) values ('" & "PRODUCTCLASS" & "','" & LblDispItem.Text.ToString & "','" & 1 & "','" & ComboDept.SelectedValue.ToString & "')")
Conn.Open();
cmdSave.ExecuteNonQuery();

        cmdSave.CommandText = ("insert into EXT00101([PT_window_ID],[PT_UD_Key],[PT_UD_Number],[STRGA255]) values ('" & "PRODUCTCLASS" & "','" & LblDispItem.Text.ToString & "','" & 2 & "','" & CmbSubDept.SelectedValue.ToString & "')")
Conn.Open();
cmdSave.ExecuteNonQuery();
        cmdSave.CommandText = ("insert into EXT00101([PT_window_ID],[PT_UD_Key],[PT_UD_Number],[STRGA255]) values ('" & "PRODUCTCLASS" & "','" & LblDispItem.Text.ToString & "','" & 3 & "','" & CmbClass.SelectedValue.ToString & "')")
Conn.Open();
cmdSave.ExecuteNonQuery();
        cmdSave.CommandText = ("insert into EXT00101([PT_window_ID],[PT_UD_Key],[PT_UD_Number],[STRGA255]) values ('" & "PRODUCTCLASS" & "','" & LblDispItem.Text.ToString & "','" & 4 & "','" & CmbSubClass.SelectedValue.ToString & "')")
Conn.Open();
cmdSave.ExecuteNonQuery();

Conn.Close();

Open in new window

0
 

Accepted Solution

by:
farooque earned 0 total points
Comment Utility
I was able to do it this way

Dim cmdSaveDept As New SqlCommand
        cmdSaveDept.Connection = _gpSqlConnection
        cmdSaveDept.CommandType = CommandType.StoredProcedure
        cmdSaveDept.CommandText = "SD_InsertTaxonomyValues"
        cmdSaveDept.Parameters.Add("@ITEMNMBR", SqlDbType.VarChar).Value = LblDispItem.Text
        cmdSaveDept.Parameters.Add("@Department", SqlDbType.VarChar).Value = TxtDept.Text 'ComboDept.SelectedValue("Department").ToString
        cmdSaveDept.Parameters.Add("@SubDept", SqlDbType.VarChar).Value = CmbSubDept.SelectedValue("SubDeptname").ToString
        cmdSaveDept.Parameters.Add("@Class", SqlDbType.VarChar).Value = CmbClass.SelectedValue("ClassName").ToString
        cmdSaveDept.Parameters.Add("@SubClass", SqlDbType.VarChar).Value = CmbSubClass.SelectedValue("SubClassName").ToString
        If _gpSqlConnection.State <> ConnectionState.Open Then _gpSqlConnection.Open()
        Try
            cmdSaveDept.ExecuteNonQuery()
            ' MsgBox("Taxonomy Fields Added Successfully ", MsgBoxStyle.OkOnly)
            Me.Close()
        Catch ex As Exception
            MsgBox("Please Check, Duplicate Information, Record Already Exists" & vbCrLf & ex.Message & vbCrLf & _gpSqlConnection.ConnectionString, MsgBoxStyle.Critical)
        End Try
0
 

Author Closing Comment

by:farooque
Comment Utility
Good Suggestion, Changed a bit and was able to get it
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now