Click here to Skip to main content
16,013,207 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox x:Name="ComboBox1" VerticalAlignment="Center" Width="200" Height="30" Margin="0,0,0,70" HorizontalAlignment="Center" IsEditable="False" SelectionChanged="cmbSelectCity_SelectionChanged" >
            <ComboBoxItem>
                <CheckBox x:Name="chkBostan" Content="Bostan" />
            </ComboBoxItem>
            <ComboBoxItem>
                <CheckBox x:Name="chkAlaska" Content="Alaska" />
            </ComboBoxItem>
            <ComboBoxItem>
                <CheckBox x:Name="chkArizona" Content="Arizona" />
            </ComboBoxItem>
            <ComboBoxItem>
                <CheckBox x:Name="chkFlorida" Content="Florida" />
            </ComboBoxItem>
            <ComboBoxItem>
                <CheckBox x:Name="chkCalifornia" Content="California" />
            </ComboBoxItem>
            <ComboBoxItem>
                <CheckBox x:Name="chkClorado" Content="Clorado" />
            </ComboBoxItem>
        </ComboBox>
        <Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="397,110,0,0" x:Name="Submit" VerticalAlignment="Top" Width="76" Click="Submit_Click" Background="#FF8F9AAB" FontSize="14" />
    </Grid>
</Window>



SQL
Create table person
(per_id numeric(10) null,
 per_name varchar(max)null ,
 per_city varchar(max) null,
);


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        string dbconnection = (@"Data Source=EMIII-PC; Initial Catalog=gymdatabase;Integrated Security=True");

    

        private void cmbSelectCity_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }



        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(dbconnection))
            {
                try
                {


                    {
                        conn.Open();
                        using (SqlCommand cmd = new SqlCommand(@"
                            INSERT INTO person
                            (
                             per_city
                            )
                            VALUES  
                            (
                              @percity
                            )", conn))
                        {
                           

                            cmd.Parameters.AddWithValue("@percity", ComboBox1.Text);


                            int rows = cmd.ExecuteNonQuery();
                           
                         
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }

                finally
                {
                    conn.Close();
                }
            }
        }
    }
}
Posted
Comments
[no name] 9-Jul-14 7:20am    
http://www.codeproject.com/Questions/794600/My-All-Code-Is-Working-Perfectly-Accept-My-Combobo?arn=0
Member 10925486 9-Jul-14 7:25am    
that link solution not working for me
[no name] 9-Jul-14 7:30am    
"Not working" means absolutely nothing to anyone but you.
Reposting the same question over and over does not help you.
If you want to same "multiple comma separated" values (why you would even want to do this is a mystery) then go back to the first "question" and do what you have already been told to do. It's not likely that you will get any more different answers.
Member 10925486 9-Jul-14 7:22am    
how can i insert Boston an Alaska with comma into single column name per_city of table person ....my this code show null space in database in per_city . right now i dont want how to save per_id or per_name i just want to know how to save combobox multiselected checkbox values in database single column .... plz plz can any one edited this code with due to my requirement i will be very thankfull for him

1 solution

Create Procedure-------------

Create procedure SP_Test_XMLInsert
(
@xmlstr ntext,
@Status int output
)
as
begin
declare @hDoc int
exec sp_xml_preparedocument @hDoc OUTPUT,@xmlstr
insert into person
select xml.per_id,xml.per_name,xml.per_city
from OPENXML(@hDoc,N'//Master',2)
with(
per_id numeric(10) '@per_id',
per_name varchar(max) '@per_name',
per_city varchar(max) '@per_city'
)xml
exec sp_xml_removedocument @hDoc
select @Status=SCOPE_IDENTITY();
end


Add code on button click--------------------
-------------------------------------------------------------
protected void Save_Record(object sender, EventArgs e)
{
#region —- Input XML —-
XElement input_RQ = new XElement("Request");
#region —- Master Node —-
foreach (ListItem lst in chkBranchList.Items)
{

if (lst.Selected)
{
string str=lst.Value;
XElement xMaster = new XElement("Master", new XAttribute("per_id", "123"),
new XAttribute("per_name", "1234"),
new XAttribute("per_city", "Delhi"));
input_RQ.Add(xMaster);
}
}
#endregion

#endregion

#region ————- Insert————–
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = ConfigurationManager.ConnectionStrings["connn"].ConnectionString;
SqlCommand sqlCommand = new SqlCommand();
sqlCon.Open();
sqlCommand = new SqlCommand();
sqlCommand.CommandText = "SP_Test_XMLInsert";
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Connection = sqlCon;
sqlCommand.Parameters.AddWithValue("@xmlstr", input_RQ.ToString());
SqlParameter outParam = new SqlParameter("@Status", SqlDbType.VarChar, 50);
outParam.Direction = ParameterDirection.Output;
sqlCommand.Parameters.Add(outParam);
sqlCommand.ExecuteNonQuery();
string sMsg = sqlCommand.Parameters["@Status"].Value.ToString();

#endregion ———–Insert————–
}
 
Share this answer
 
Comments
Member 10925486 9-Jul-14 12:57pm    
suneel i tried to put your concept in my code but its give lots of error :( n i cant sort out your concept becox its lit bit different for me can you plz edited my code with your concept may be its work :( i will be very thank full to you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900