locked
How to Insert Checked List Box Values and Retrieve Value from Database RRS feed

  • Question

  • Hi... I want to create table called Subtitles in which user may select Different languages. 

    My prob is dat Which Datatype should i use for to store values in the Table (Sql Server)

    and 

    Can any one explain with the sample.. how to store checkedlistbox values to DB and Retrieve it.  With table Structre

     

     

     

    Saturday, April 3, 2010 4:20 PM

Answers

  • I might use a table to track the title->subtitle(lcid) relationship like this...

    USE [MSDN]
    GO
    
    /****** Object:  Table [dbo].[TitleLanguage]    Script Date: 04/03/2010 12:05:50 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE TABLE [dbo].[TitleLanguage](
    	[SubtitleKey] [int] IDENTITY(1,1) NOT NULL,
    	[TitleKey] [int] NOT NULL,
    	[LCID] [int] NOT NULL,
     CONSTRAINT [PK_TitleLanguage] PRIMARY KEY CLUSTERED 
    (
    	[SubtitleKey] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO

    And then a form for editing the selected subtitle(lcid) like this...

    using System;
    using System.Globalization;
    using System.Windows.Forms;
    
    namespace MSDN.Forums.Example
    {
        public partial class LanguageForm : Form
        {
            public LanguageForm()
            {
                InitializeComponent();
            }
    
            private void LanguageForm_Load(object sender, EventArgs e)
            {
                CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.FrameworkCultures);
                checkedListBox1.DataSource = cultures;
                checkedListBox1.DisplayMember = "DisplayName";
                checkedListBox1.ValueMember = "LCID";
            }
    
            private void m_ButtonSaveLanguages_Click(object sender, EventArgs e)
            {
                // TODO: Delete all current LCID records for the title
                foreach (CultureInfo culture in checkedListBox1.CheckedItems)
                {
                    int selectedLCID = culture.LCID;
                    // TODO: Write LCID record to database table for the title
                }
            }
        }
    }

     That looks like this...

    Language Selector Dialog Image


    "There's a way to do it better - find it." - Thomas Edison
    • Proposed as answer by A.m.a.L Hashim Saturday, April 3, 2010 6:07 PM
    • Marked as answer by Jing0 Friday, April 9, 2010 7:58 AM
    Saturday, April 3, 2010 5:11 PM
  • Here is how to insert data into sql table

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/91bd10ce-c83b-4766-920b-2137ddc29908

    Here is how to read data back

    http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson04.aspx

    Once you become familiar with the above, I suggest you can move to Microsoft Enterprise Library. Which is a collection of reusable software components (application blocks) designed to assist software developers with common enterprise development cross-cutting concerns (such as logging, validation, data access, exception handling, and many others). Application blocks are a type of guidance; they are provided as source code, test cases, and documentation that can be used "as is," extended, or modified by developers to use on complex, enterprise-level line-of-business development projects.

    http://msdn.microsoft.com/en-us/library/cc467894.aspx


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Marked as answer by Jing0 Friday, April 9, 2010 7:58 AM
    Saturday, April 3, 2010 6:11 PM

All replies

  • I might use a table to track the title->subtitle(lcid) relationship like this...

    USE [MSDN]
    GO
    
    /****** Object:  Table [dbo].[TitleLanguage]    Script Date: 04/03/2010 12:05:50 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE TABLE [dbo].[TitleLanguage](
    	[SubtitleKey] [int] IDENTITY(1,1) NOT NULL,
    	[TitleKey] [int] NOT NULL,
    	[LCID] [int] NOT NULL,
     CONSTRAINT [PK_TitleLanguage] PRIMARY KEY CLUSTERED 
    (
    	[SubtitleKey] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO

    And then a form for editing the selected subtitle(lcid) like this...

    using System;
    using System.Globalization;
    using System.Windows.Forms;
    
    namespace MSDN.Forums.Example
    {
        public partial class LanguageForm : Form
        {
            public LanguageForm()
            {
                InitializeComponent();
            }
    
            private void LanguageForm_Load(object sender, EventArgs e)
            {
                CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.FrameworkCultures);
                checkedListBox1.DataSource = cultures;
                checkedListBox1.DisplayMember = "DisplayName";
                checkedListBox1.ValueMember = "LCID";
            }
    
            private void m_ButtonSaveLanguages_Click(object sender, EventArgs e)
            {
                // TODO: Delete all current LCID records for the title
                foreach (CultureInfo culture in checkedListBox1.CheckedItems)
                {
                    int selectedLCID = culture.LCID;
                    // TODO: Write LCID record to database table for the title
                }
            }
        }
    }

     That looks like this...

    Language Selector Dialog Image


    "There's a way to do it better - find it." - Thomas Edison
    • Proposed as answer by A.m.a.L Hashim Saturday, April 3, 2010 6:07 PM
    • Marked as answer by Jing0 Friday, April 9, 2010 7:58 AM
    Saturday, April 3, 2010 5:11 PM
  • Here is how to insert data into sql table

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/91bd10ce-c83b-4766-920b-2137ddc29908

    Here is how to read data back

    http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson04.aspx

    Once you become familiar with the above, I suggest you can move to Microsoft Enterprise Library. Which is a collection of reusable software components (application blocks) designed to assist software developers with common enterprise development cross-cutting concerns (such as logging, validation, data access, exception handling, and many others). Application blocks are a type of guidance; they are provided as source code, test cases, and documentation that can be used "as is," extended, or modified by developers to use on complex, enterprise-level line-of-business development projects.

    http://msdn.microsoft.com/en-us/library/cc467894.aspx


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    • Marked as answer by Jing0 Friday, April 9, 2010 7:58 AM
    Saturday, April 3, 2010 6:11 PM
  • Excellent reference resources A.m.a.L.

    While the Enterprise Libraries are a powerful tool for developers, they can also be more bloated than can be justified for some solutions. EntLib Logging for example requires a large amount of configuration just for a simple text log, altough it can easily be switched to a sophisticated event log.

    Ravi, you should definately read the resources posted by A.m.a.L, but use discernment when considering the enterprise libraries. Just because something is cool, doesn't means it's right for every solution.


    "There's a way to do it better - find it." - Thomas Edison
    Sunday, April 4, 2010 2:19 PM
  • Enterprise Library is heavy, but can save a lot of your development time. It saved most of mine :-) and even we can modify it as required.
    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    Sunday, April 4, 2010 3:06 PM
  • How to Retrive it From Database.. Suppose If the user selected english, French, which is stored in the database.. so after a button click how to read the values from Database and Display in the check box list.

     

    I meant to say that... After clicking a button values stored in database, should get populated in checked  list box

    Monday, May 24, 2010 1:53 PM
  • You can query the table, and bind the source to the list box control.

    C# Tutorial - Binding a DataGridView to a Database | Switch on the

    Read data from database in code behind (C# ) : Query «

    Get data in database through SqlDataAdapter (C# ) :

     


    Thanks,
    A.m.a.L
    [MVP Visual C#]
    Dot Net Goodies
    Don't hate the hacker, hate the code
    Tuesday, May 25, 2010 5:32 AM