Jawab Displaying field to textbox

  • 11 Juli 2012 1:20
     
     

    I have 2 tables which are contain ID field, on the second table I want to add a record based on ID from table 1 so I provide users to input on a text box for ID and the field Subject will appear on another the text box, here are my code that throws an exception for declaring scalar variable @ID

     private void textBox6_KeyDown(object sender, KeyEventArgs e)
            {
                if (radioButton2.Checked)
                {

                    if (e.KeyCode == Keys.Enter)
                    {
                        try
                        {
                            SqlCommand perintahsql = new SqlCommand("select Subject from jadwalkuliah where ID = @ID", koneksi);
                            SqlParameter parameter = new SqlParameter();
                            parameter.ParameterName = "@ID";
                            parameter.SqlDbType = SqlDbType.Int;
                            parameter.Direction = ParameterDirection.Input;
                            parameter.Value = textBox6.ToString();
                            koneksi.Open();
                            SqlDataReader bacadata = perintahsql.ExecuteReader();
                            if (bacadata.HasRows)
                            {
                                while (bacadata.Read())
                                {
                                    textBox2.Text = bacadata["Subject"].ToString();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex + "ex");
                        }
                        koneksi.Close();
                    }
                }
            }



Semua Balasan