Solved

Saving Selected Combobox Values to a SQL Server Table

Posted on 2012-03-20
8
259 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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…

831 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