Results 1 to 5 of 5

Thread: Database handle

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Location
    Dhaka, Bangladesh
    Posts
    102

    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

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Location
    Dhaka, Bangladesh
    Posts
    102

    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

  4. #4
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Database handle

    sorry about my post i'm still a newbie here, but maybe this would help.
    VB Code:
    1. SqlConnection conn = new SqlConnection("user id=sa;password=password;initial catalog=northwind");
    2.         private void btnok_click(object sender, System.EventArgs e)
    3.         {
    4.             try
    5.             {
    6.                 conn.Open();
    7.                 string cmdtext = "insert into student(Idno,Lastname,Firstname)values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
    8.                 SqlCommand sqlCom = new SqlCommand(cmdtext,conn);
    9.                 sqlCom.ExecuteNonQuery();
    10.             }
    11.             catch(Exception ex)
    12.             {
    13.                 MessageBox.Show(ex.Message);
    14.             }
    15.             finally
    16.             {
    17.                 conn.Close();
    18.             }
    19.         }

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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
  •  



Click Here to Expand Forum to Full Width