Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
sir i have 30 controls in my window base form. some are checkboxes some are comboboxes and some are textboxes. and one submit button .
i want whichever i filled and checked and type in the textboxes only those controls data should be insert and rest of controls fields data should be insert empty inside the data table . how to do.
if is there any example so please forward the link.

What I have tried:

please forward the links which is related to this exampml
Posted
Updated 25-Feb-16 1:16am
Comments
dan!sh 25-Feb-16 6:46am    
If there is no data input in control, nothing will be inserted. Isn't it?
[no name] 25-Feb-16 7:07am    
If you are inserting all controls value to DB then what is point of asking checked controls and rest of controls.
faizyab 2009 26-Feb-16 1:01am    
public partial class Form8 : Form
{
public Form8()
{
InitializeComponent();
}

string partname;
private void Form8_Load(object sender, EventArgs e)
{

}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
partname = "HardDrive";
}

private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
partname = "Mouse";
}

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
partname = "RAM";
}

private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
partname = "KeyBoard";
}

private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
partname = "Monitor";
}

private void checkBox6_CheckedChanged(object sender, EventArgs e)
{
partname = "DVD Writer";
}

private void checkBox7_CheckedChanged(object sender, EventArgs e)
{
partname = "UPS";
}

private void checkBox8_CheckedChanged(object sender, EventArgs e)
{
partname = "Printer";
}

private void checkBox9_CheckedChanged(object sender, EventArgs e)
{
partname = "Lan Card";
}

private void button1_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DCID;Integrated Security=True"))
{
string query = "insert into Direct_Purchase_Accessories(product,make,serialnumber) values(@product,@make,@serialnumber)";
SqlCommand cmd = new SqlCommand(query, con);
foreach (Control control in Controls)
{
if (control is CheckBox)
{

CheckBox chk = new CheckBox();
chk.Checked = Convert.ToBoolean(control);
cmd.Parameters.AddWithValue("@product", chk.Checked);
}
}

foreach (Control control1 in Controls)
{
if (control1 is ComboBox)
{

ComboBox combo = new ComboBox();
combo.SelectedItem = control1;
cmd.Parameters.AddWithValue("@make", combo.SelectedItem);
}
}

foreach (Control control2 in Controls)
{
if (control2 is TextBox)
{



cmd.Parameters.AddWithValue("@serialnumber", control2 as TextBox);
}
}





SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Direct_Purchase_Accessories");
MessageBox.Show("submitted");



}
}
catch (Exception ex)
{
MessageBox.Show("Please check"+ex);

}
}

private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{

}
}

1 solution

There is no built-in way to do this, you just have to write the code. But from what you describe, it should already happen. If the controls are empty when you go and add them as parameters to a stored procedure the value will be empty. If you don't want to empty the actual values then you need to either change the stored procedure to check for nulls or you need to change how you are calling your database. Either way, you have to decide and then do it. No built-in way.
 
Share this answer
 
Comments
faizyab 2009 25-Feb-16 14:05pm    
public partial class Form8 : Form
{
public Form8()
{
InitializeComponent();
}

string partname;
private void Form8_Load(object sender, EventArgs e)
{

}

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
partname = "HardDrive";
}

private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
partname = "Mouse";
}

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
partname = "RAM";
}

private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
partname = "KeyBoard";
}

private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
partname = "Monitor";
}

private void checkBox6_CheckedChanged(object sender, EventArgs e)
{
partname = "DVD Writer";
}

private void checkBox7_CheckedChanged(object sender, EventArgs e)
{
partname = "UPS";
}

private void checkBox8_CheckedChanged(object sender, EventArgs e)
{
partname = "Printer";
}

private void checkBox9_CheckedChanged(object sender, EventArgs e)
{
partname = "Lan Card";
}

private void button1_Click(object sender, EventArgs e)
{
try
{
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DCID;Integrated Security=True"))
{
string query = "insert into Direct_Purchase_Accessories(product,make,serialnumber) values(@product,@make,@serialnumber)";
SqlCommand cmd = new SqlCommand(query, con);
foreach (Control control in Controls)
{
if (control is CheckBox)
{

CheckBox chk = new CheckBox();
chk.Checked = Convert.ToBoolean(control);
cmd.Parameters.AddWithValue("@product", chk.Checked);
}
}

foreach (Control control1 in Controls)
{
if (control1 is ComboBox)
{

ComboBox combo = new ComboBox();
combo.SelectedItem = control1;
cmd.Parameters.AddWithValue("@make", combo.SelectedItem);
}
}

foreach (Control control2 in Controls)
{
if (control2 is TextBox)
{



cmd.Parameters.AddWithValue("@serialnumber", control2 as TextBox);
}
}





SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "Direct_Purchase_Accessories");
MessageBox.Show("submitted");



}
}
catch (Exception ex)
{
MessageBox.Show("Please check"+ex);

}
}

private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{

}
}

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