Solved

Saving Selected Combobox Values to a SQL Server Table

Posted on 2012-03-20
8
261 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Input parameteres to DragOver 2 19
RAISERROR WITH NOWAIT 2 16
HTML <font style="color:red"> 9 31
Isolation level setting TSQL View 10 24
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

839 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