Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey,


C#
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=AG_FAT;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("insert into Fatura(Kimlik,No,Date,Total,Type,D1,T1,D2,T2,D3,T3,Rest,Info) values(@Kimlik,@No,@Date,@Total,@Type,@D1,@T1,@D2,@T2,@D3,@T3,@Rest,@Info)", con);
cmd.Parameters.AddWithValue("@Kimlik", textBox1.Text);
cmd.Parameters.AddWithValue("@No", textBox2.Text);
cmd.Parameters.AddWithValue("@Date", textBox3.Text);
decimal total = decimal.Parse(textBox5.Text);
cmd.Parameters.AddWithValue("@Total", total);
cmd.Parameters.AddWithValue("@Type", textBox6.Text);
cmd.Parameters.AddWithValue("@D1", textBox7.Text);
decimal t1 = decimal.Parse(textBox10.Text);
cmd.Parameters.AddWithValue("@T1", t1);
cmd.Parameters.AddWithValue("@D2", textBox8.Text);
decimal t2 = decimal.Parse(textBox11.Text);
cmd.Parameters.AddWithValue("@T2", t2);
cmd.Parameters.AddWithValue("@D3", textBox9.Text);
decimal t3 = decimal.Parse(textBox12.Text);
cmd.Parameters.AddWithValue("@T3", t3);
decimal rest = decimal.Parse(textBox13.Text);
cmd.Parameters.AddWithValue("@Rest", rest);
cmd.Parameters.AddWithValue("@Info", textBox4.Text);

cmd.ExecuteNonQuery();
SqlDataReader dr1;
try
{
    dr1 = cmd.ExecuteReader();
    while(dr1.Read())
    {

        comboBox1.Items.Add(dr1[0]);
        con.Close();
    }

}
catch (Exception es)
{
    MessageBox.Show(es.Message, "FAIL");
}
con.Close();
MessageBox.Show("SUCCESS");



I'm just trying to add values to sql by filling textboxes and a combobox. I don't know why but when i click on "Add" Button on my software, nothing adds on my combobox but same 2 row is going to add on SQL Table. I want to add all textbox info to table on SQL and add only "Kimlik" column to combobox.
Posted

1 solution

I think you must run another query that selects you column "SELECT COLUMN_NAME FROM TABLE_NAME" execute it in another call then fetch the result using

dr = cmd2.ExecuteReader();
while(dr.Read())
{
comboBox1.Items.Add(dr[0]);
con.Close();
}

you must execute a select query after your insert query.
 
Share this answer
 
Comments
Member 10587827 9-Jan-15 10:03am    
it worked thanks but it worked when i change [0] to [Column_name]

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