?
Solved

Saving Selected Combobox Values to a SQL Server Table

Posted on 2012-03-20
8
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37744422
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
ID: 37745001
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
ID: 37745689
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:farooque
ID: 37749037
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
 

Author Comment

by:farooque
ID: 37750382
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
ID: 37751100
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
ID: 37814568
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
ID: 37826822
Good Suggestion, Changed a bit and was able to get it
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

752 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