I am new in C#.
How can I connect witha sql server database from C# when a form is loaded.
How can I add new row in the database table when I click an button
from the form.
Please help me.
Rajib
Printable View
I am new in C#.
How can I connect witha sql server database from C# when a form is loaded.
How can I add new row in the database table when I click an button
from the form.
Please help me.
Rajib
Thank you for giving that useful link.
I am trying with your suggession. It is good.
But I am in some trouble to handle it.
I do not know when I write code like the tutorial my program says an error!
It says that System.Data.SqlClient does not contain conn.close();
Please help me.
I am really new in this environment. and I am trying to save data from a form to a database table in SQL.
Rajib
sorry about my post i'm still a newbie here, but maybe this would help.
:afrog:VB Code:
SqlConnection conn = new SqlConnection("user id=sa;password=password;initial catalog=northwind"); private void btnok_click(object sender, System.EventArgs e) { try { conn.Open(); string cmdtext = "insert into student(Idno,Lastname,Firstname)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')"; SqlCommand sqlCom = new SqlCommand(cmdtext,conn); sqlCom.ExecuteNonQuery(); } catch(Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Close(); } }
Show the erronous code if you still can't get it to work .