This is a simple one.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection myConnection = new SqlConnection("user id=username;" +
"password=password;server=serverurl;" +
"Trusted_Connection=yes;" +
"database=database; " +
"connection timeout=30");
private void Form1_Load(object sender, EventArgs e)
{
BindAuthors();
}
void BindAuthors()
{
try
{
myConnection.Open();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
SqlCommand myCommand = new SqlCommand("SELECT * FROM [author_master]", myConnection);
SqlDataAdapter ta = new SqlDataAdapter(myCommand);
DataTable dt = new DataTable();
ta.Fill(dt);
cmbAuthor.ValueMember = "c_code";
cmbAuthor.DisplayMember = "c_author";
cmbAuthor.DataSource = dt;
myConnection.Close();
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
myConnection.Open();
}
catch (Exception ex)
{
Console.WriteLine(e.ToString());
}
SqlCommand myCommand = new SqlCommand("INSERT INTO [book_master] (c_name,c_author) VALUES ('" + txtBookName.Text + "','" + cmbAuthor.SelectedValue + "')", myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}