|
-
Apr 5th, 2005, 12:28 AM
#1
Thread Starter
Lively Member
Database handle
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
-
Apr 5th, 2005, 12:38 AM
#2
-
Apr 5th, 2005, 05:39 AM
#3
Thread Starter
Lively Member
Re: Database handle
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
-
Apr 5th, 2005, 07:32 AM
#4
Hyperactive Member
Re: Database handle
sorry about my post i'm still a newbie here, but maybe this would help.
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();
}
}
-
Apr 6th, 2005, 03:46 AM
#5
Sleep mode
Show the erronous code if you still can't get it to work .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|