Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to fill one checkbox with some values I saved inside an SQL database.

C#
var numfatura = _transaction.TransDocument + _transaction.TransSerial + _transaction.TransDocNumber;


The variable I need it this one and I saved it inside the SQL like this:

C#
using (SqlConnection conn = new SqlConnection(@"Data source = 2c4138928627\Sage ; Database=ARMINDOData ; User Id=sa ; Password=sage2008+"))
{
 SqlCommand command = new SqlCommand("IF OBJECT_ID('UXFaturas', 'U') IS NULL CREATE TABLE UXFaturas(NumFaturas char(250), Cont1 char(250), Cont2 char(250));", conn);
 conn.Open();
 SqlCommand insertCommand = new SqlCommand("INSERT INTO UXFaturas(NumFaturas, Cont1, Cont2) VALUES (numfatura, cont, cont2)", conn);
    command.ExecuteNonQuery();
    command.Connection = conn;
    command.CommandType = System.Data.CommandType.StoredProcedure;
    //command.CommandText = "USP_InsertFile";
    command.Parameters.AddWithValue("@numfaturas", numfatura);
    command.Parameters.AddWithValue("@cont1", cont);
    command.Parameters.AddWithValue("@cont2", cont2);

  //command.ExecuteNonQuery();
 }
}


What I have tried:

I've tried to seach in internet but didnt find anything that was my problem and I have no clue on what should I start. I had this back in the day but I changed my project so this doesnt work anymore but may help:
C#
string path = @"C:\Users\HP8200\Desktop\";
 var files = System.IO.Directory.GetFiles(path, "*.txt");
 foreach (var item in files)
 {
 string fileNameOnly = "";
 fileNameOnly = Path.GetFileNameWithoutExtension(item);
 checkedListBox1.Items.Add(fileNameOnly); 
}


I'm using c#.
Posted
Updated 17-Aug-17 7:25am
v2
Comments
Prifti Constantine 17-Aug-17 5:57am    
The database is the txt file that you read with GetFiles() method?
Member 13356973 17-Aug-17 6:32am    
The GetFiles() method is what I used when I created the textfile, now i dont create any textfile, I just save it into the sql server.
Prifti Constantine 17-Aug-17 6:37am    
Lets say that the data is already in the sql server.... The command you are using here is an Insert Command... In order to fill the checkboxes you need to retrieve Data from the database, not insert it which in this case it is a simple Select Statement. Is this the appropriate (for you) code that you wish to provide? Maybe you forgot something?
Member 13356973 17-Aug-17 6:48am    
The insert that you see in the question is to insert the data into the sql database. I think I didnt forget anything, but if you need something else I can provide you.
Locura The Exile 17-Aug-17 6:48am    
As mentioned by @Prifti, the code you provided is for inserting, can you also provide the code with which you are currently retrieving the data and what errors/problems you are getting from it?

1 solution

Hi,

An Example to help you.
C#
private DataTable SelectData()
{ 
            DataTable dt = null;
            using (SqlConnection conn = new SqlConnection(@"Data source = 2c4138928627\Sage ; Database=ARMINDOData ; User Id=sa ; Password=sage2008+"))
            {
                SqlCommand command = new SqlCommand("SELECT myfieldtext, myfieldvalue FROM mytable", conn);
                conn.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(dt);
            }

            return dt;
}

private void FillListBoxCheck()
{
            DataTable dt = this.SelectData();

            this.CheckBoxList1.DataTextField = "myfieldtext";
            this.CheckBoxList1.DataValueField = "myfieldvalue";
            this.CheckBoxList1.DataSource = dt;
            this.CheckBoxList1.DataBind();
 
}
 
Share this answer
 

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