Click here to Skip to main content
15,918,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I want to perform an insert the data and view the data . I had few multiple checkboxes in my form. I inserted the data in database now i need to view the inserted datas. Pls give me suggestion.



Thank you.

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
if (validate()==1)
{

con.Open();
string query = "insert into general_ward values (@fno,@rtype,@tbed,@incharge,@abed,@specialites)";
cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@fno",textBox1.Text);
cmd.Parameters.Add("@rtype", comboBox2.Text);
cmd.Parameters.Add("@tbed", textBox2.Text);
cmd.Parameters.Add("@incharge", textBox3.Text);
cmd.Parameters.Add("@abed", textBox4.Text);
string s = spl();
cmd.Parameters.Add("@specialites", s);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Added");
cmd.Dispose();
con.Close();
}
}





private void button2_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand ("select * from general_ward where fno='"+textBox1.Text+"'",con);
SqlDataReader dr = cmd.ExecuteReader();
if(dr.Read())

{
string aa = dr["specialites"].ToString();
string[] a = aa.Split('/');
foreach(Control cc in this.Controls)
{
if(cc is CheckBox)
{
CheckBox spl = (CheckBox)cc;
for(int j=0;j<a.length;j++)>
{
if (a[j].ToString()==spl.Text)
{
spl.Checked= true;
}
}
}
}
}
con.Close();
}
Posted
Updated 9-Sep-16 19:25pm
Comments
Karthik_Mahalingam 10-Sep-16 1:08am    
post the spl() code
win or web?
GKRISH04 10-Sep-16 1:10am    
public string spl()
{
string spl = "";

if (checkBox1.Checked == true)
{
spl = spl + checkBox1.Text + "/";
}
if (checkBox2.Checked == true)
{
spl = spl + checkBox2.Text + "/";
}
if (checkBox3.Checked == true)
{
spl = spl + checkBox3.Text + "/";
}
if (checkBox4.Checked == true)
{
spl = spl + checkBox4.Text + "/";
}
if (checkBox5.Checked == true)
{
spl = spl + checkBox5.Text + "/";
}
if (checkBox6.Checked == true)
{
spl = spl + checkBox6.Text + "/";
}
if (checkBox7.Checked == true)
{
spl = spl + checkBox7.Text + "/";
}
if (checkBox8.Checked == true)
{
spl = spl + checkBox8.Text + "/";
}
if (checkBox9.Checked == true)
{
spl = spl + checkBox9.Text + "/";
}
if (checkBox10.Checked == true)
{
spl = spl + checkBox10.Text + "/";
}
if (checkBox11.Checked == true)
{
spl = spl + checkBox11.Text + "/";
}
return spl;
}
GKRISH04 10-Sep-16 1:11am    
win
Karthik_Mahalingam 10-Sep-16 1:14am    
why dont you use checkedlistbox?
GKRISH04 10-Sep-16 1:18am    
Okay i'll try . Any error on my coding ?

1 solution

try this

C#
private void SetSelected(string selected)
       {
           CheckBox[] myCheckBoxes = { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10 };
           string[] selectedItems = selected.Split('/');
           myCheckBoxes.ToList().ForEach(k =>
           {
               k.Checked = false;
               if (selectedItems.Contains(k.Text))
                   k.Checked = true;
           });

       }

       private string GetSelected()
       {
           CheckBox[] myCheckBoxes = { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8, checkBox9, checkBox10 };
           string selected = "";
           myCheckBoxes.ToList().ForEach(k =>
           {
               if (k.Checked)
                   selected += k.Text + "/";
           });
           return selected.TrimEnd('/');
       }
 
Share this answer
 
Comments
GKRISH04 10-Sep-16 2:13am    
Thank you bro. Its worked.
Karthik_Mahalingam 10-Sep-16 4:36am    
welcome :)

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