CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Inserting data

  1. #1
    Join Date
    Jul 2007
    Location
    In the present
    Posts
    80

    Question Inserting data

    The code below is for a button which needs to send data to a sql DB

    I have two questions:

    1 Does anyone have a sample code snippet that i can look at to see how to do this

    2 would it be better to pass the variables to a stored procedure
    and have the DB do the insert
    or
    should i just code it in as an insert into


    My initial instinct is to use a stored procedure as that would make it easier
    to upgrade/change in the future if needed.

    Code:
    private void Submit_btn_Click_1(object sender, EventArgs e)
            {
                //submit data to the desc_table
                /*connecting to database to perform task
                *  connection variable. open();
                *   function
                * .close(); */
    
                myconnection.Open();
                //do stuff
                myconnection.Close();
            }

    Thanks all in advance

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto
    Posts
    805

    Re: Inserting data

    stored procedures are a bit better, that way if you got to change something you can usually get away without having to re-comple your app.

    firstly you need to define what your using. You going to use Sql or ODBC?

    heres a MSDN Sample : http://msdn2.microsoft.com/en-us/lib...88(VS.71).aspx
    hth,
    mcm
    rate my posts!
    mcm

  3. #3
    Join Date
    Jul 2007
    Location
    In the present
    Posts
    80

    Re: Inserting data

    Quote Originally Posted by mcmcom
    stored procedures are a bit better, that way if you got to change something you can usually get away without having to re-comple your app.

    firstly you need to define what your using. You going to use Sql or ODBC?

    heres a MSDN Sample : http://msdn2.microsoft.com/en-us/lib...88(VS.71).aspx
    hth,
    mcm

    I am using SQL server. Thanks for the link but that is for an asp.net page and I am building a windows app

  4. #4
    Join Date
    Jun 2003
    Location
    Toronto
    Posts
    805

    Re: Inserting data

    well its pretty much the exact same code for a windows form, the calls to SQL server are irralavant to the fact that its win forms or a web app. you still need a :

    Connection Object
    Command Object
    Parameters
    Execution block

    Code:
    using(SqlConnection conn = new SqlConnection(ConfigruationManager.ConnectionStrings["myConStr"])){ 
     
     SqlCommand cmd  = new SqlCommand("INSERT STATEMENT", conn); 
    cmd.Parameters.AddWithValue("@myParm1",Value1); 
    ....
    try{ 
       conn.open(); 
       cmd.ExecuteNonQuery(); 
       conn.close()
    }
    catch(SqlException ex){ 
       throw(ex);
    } 
    }
    hth,
    mcm

    }
    rate my posts!
    mcm

  5. #5
    Join Date
    Jul 2007
    Location
    In the present
    Posts
    80

    Smile Re: Inserting data

    Thanks
    I will give it a try and let you know how it goes.

  6. #6
    Join Date
    Jul 2007
    Location
    In the present
    Posts
    80

    Re: Inserting data

    Well I have gotten the code to compile.. thats good but when I Start debugging I get the following error
    No mapping exists from object type System.Windows.Forms.TextBox to a known managed provider native type.
    from the line
    Code:
    Submit_btn.ExecuteNonQuery();
    Not really sure what is causing this. Here is the code for the button

    Code:
    private void Submit_btn_Click(object sender, EventArgs e)
            {
                myconnection.Open();
    
                SqlCommand Submit_btn = new SqlCommand("mp_submit", myconnection);
                Submit_btn.CommandType = CommandType.StoredProcedure;
    
                //Submit_btn.Parameters.AddWithValue("@Description", Desc_cb);
                Submit_btn.Parameters.AddWithValue("@Call", Call_box);
    
                Submit_btn.ExecuteNonQuery();
                myconnection.Close();
            }
    I even commented out a line and rewrote the stored procedure and created a new table just to see if I could insert anything.

  7. #7
    Join Date
    Jul 2007
    Location
    In the present
    Posts
    80

    Re: Inserting data

    Thanks for all the help!!

    I went back over the code and someone noticed that I had forgotten the
    Code:
     .text
    in the line
    Code:
    Submit_btn.Parameters.AddWithValue("@Call",Call_box.Text)

  8. #8
    Join Date
    Jun 2003
    Location
    Toronto
    Posts
    805

    Re: Inserting data

    just a quick note. i would not name your Sql Command after your button. It may end up causing confusion when you look and notice you have multiple objects with the same name.

    just name the command cmd or something like that

    SqlCommand cmd = new SqlCommand <- lots of people just use this.

    mcm
    rate my posts!
    mcm

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

Featured