C# with SQL [Window Forms Help]

Discussion in 'C#' started by faizulhaque, Sep 12, 2009.

  1. faizulhaque

    faizulhaque New Member

    Joined:
    May 23, 2008
    Messages:
    210
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Karachi
    Home Page:
    http://www.google.com
    Hello Every C# PRogrammer and Others:

    I need help in C# database programming. i need to save textbox text into table of c#
    Here is code
    Code:
    // create command
             SqlConnection conn = new SqlConnection(@"server = .\sqlexpress;integrated security = true;database = office");
             conn.Open();
             string query = @"insert into emp('"+txtid.Text+"','"+txtname.Text+"','"+txtdesig.Text+"','"+txtsal.Text+"','"+txtjoin.Text+"','"+txtadd.Text+"','"+txtjoin.Text+"','"+txtcont.Text+"');";
             SqlCommand cmd = new SqlCommand(query, conn);
             cmd.ExecuteNonQuery();
             conn.Close();
             
    This code is In Save Button:

    but when i execute this always Getting Error: as incorrrect Syntex.

    Any Help suggestion
     
  2. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    yes, you will get an error of Incorrect Syntax, because the Insert statement is not correct.

    The correct syntax would be:
    1] If you want to insert all values
    Code:
    "insert into emp values('"+txtid.Text+"','"+txtname.Text+"','"+txtdesig.Text+"','"+txtsal.Text+"','"+txtjoin.Text+"','"+txtadd.Text+"','"+txtjoin.Text+"','"+txtcont.Text+"');";
    2] If you want to insert specific values
    Code:
    "insert into emp(id,name,desig,sal) values('"+txtid.Text+"','"+txtname.Text+"','"+txtdesig.Text+"','"+txtsal.Text+"');";
     
    shabbir likes this.
  3. faizulhaque

    faizulhaque New Member

    Joined:
    May 23, 2008
    Messages:
    210
    Likes Received:
    3
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Karachi
    Home Page:
    http://www.google.com
    Issue Solved THanks Mate:
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice