Solved

Saving Selected Combobox Values to a SQL Server Table

Posted on 2012-03-20
8
262 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query for highest sequence 4 63
removing unwanted rows from an sql server ranked table 13 36
SQL Select Query help 1 38
Need to trim my database size 9 24
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

749 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