4 Replies - 537 Views - Last Post: 30 July 2015 - 11:22 AM Rate Topic: -----

#1 klimba  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-July 15

problem with insert records into table

Posted 30 July 2015 - 09:26 AM

hi I have the problem with insert into table a record

I created a form
<table style="width: 100%;">
        <tr>
            <td>&nbsp;Nazwa
                
            </td>
            <td>&nbsp;<asp:TextBox ID="tbPodmiotNazwa" runat="server"></asp:TextBox></td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;Adres</td>
            <td>&nbsp;<asp:TextBox ID="tbPodmiotAdres" runat="server"></asp:TextBox></td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;Województwo</td>
            <td>&nbsp;<asp:DropDownList ID="ddlWojewodztwa" runat="server" AutoPostBack="True"  DataTextField="WojewodztwoNazwa" DataValueField="WojewodztwoId"  onselectedIndexChanged="ddlWojewodztwa_SelectedIndexChanged"></asp:DropDownList></td>
            <td>&nbsp;</td>
        </tr>
<tr>
            <td colspan="2">
                <asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" /></td>
            <td>&nbsp;</td>
        </tr>
</table>



in procedure button_click I put into some code

 protected void Button2_Click(object sender, EventArgs e)
        {
           string PodmiotNazwa = tbPodmiotNazwa.Text;
            string PodmiotAdres = tbPodmiotAdres.Text;
            int PodmiotMiastoId = ddlMiasta.SelectedIndex;
            

            string CS = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            SqlConnection connection = new SqlConnection(CS);
            string zapytanie = "INSERT INTO Podmioty(PodmiotNazwa,PodmiotAdres,PodmiotMiastoId)VALUES('" + PodmiotNazwa + "','" + PodmiotAdres + "'," + PodmiotMiastoId + ")";
            SqlDataAdapter daKategorie =
                    new SqlDataAdapter(zapytanie, connection);
           
        }



and there are not new redord into table Podmioty

any sugestions ?

Is This A Good Question/Topic? 0
  • +

Replies To: problem with insert records into table

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13485
  • View blog
  • Posts: 53,845
  • Joined: 12-June 08

Re: problem with insert records into table

Posted 30 July 2015 - 09:28 AM

Why would you be using an adapter? Why not a sqlcommand object?
When do you open your connection?
Why isn't this all in a try/catch?
Was This Post Helpful? 1
  • +
  • -

#3 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 446
  • View blog
  • Posts: 2,177
  • Joined: 07-April 08

Re: problem with insert records into table

Posted 30 July 2015 - 09:52 AM

As modi said you don't need a SQLDataAdapter. With that your actually not using it properly. You pass it a Select command and it can generate the INSERT/UPDATE commands for you. What you need is a SQLCommand object. Also you should be using Parameretized queries to protect against SQL Injection.

EX:

//Using can be used because SqlConnection and SqlCommand implement IDisposable which 
            //Get's called when the using statement is done.  This takes care of having to close
            //and dispose of the objects for you. 
            using (var con = new SqlConnection(cn))
            {
                using (var cmd = new SqlCommand("<sproc name> or <Sql Query>",con))
                {
                    con.Open(); //You need to open the connection or else it can't connect.

                    /* This tells the command that it's executing a SQL String against the database and not a Stored Procedure or Direct Table Access.
                     * Options are 
                     * CommandType.Text
                     * CommandType.StoredProcedure
                     * CommandType.TableDirect
                     */
                    cmd.CommandType = CommandType.Text;  
                    
                    //Parameters.AddWithValue will replace a parameter from the SqlString above with the value you give it.  
                    //This will also sanitize it so that if I sent in 1'; DROP TABLE FOO; -- It would take it literally and not as multiple commands
                    cmd.Parameters.AddWithValue("@Param1", "Value1");

                    /* This will execute the Command you told it to use.  
                     * For INSERT, UPDATE, and DELETE You can use 
                     * ExecuteNonQuery(); Because it does not return data.
                     * For SELECT you can use one of the following:
                     * cmd.ExecuteReader();
                     * cmd.ExecuteScalar();
                     * cmd.ExecuteXmlReader();
                     */
                    
                    cmd.ExecuteNonQuery();  
                }
            }


Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,372
  • Joined: 02-June 10

Re: problem with insert records into table

Posted 30 July 2015 - 09:59 AM

We have a lot of good tutorials on databases.
I suggest you stop trying to build for the moment.
Spend a few hours LEARNING the basics. Then come back to your project with more understanding. The time spent learning will pay off 100 times when it comes time to design then build.

See FAQ #31 for a number of links to tutorials.



tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.
Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute, check the condition of each of variable's run-time value, and watch the logic unfold right before your eyes.
Visualizing what your code does will let you see why it behaves the way it does.
It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

In addition to FAQ 2 in the list below, also check my signature block for a link on how to look at your variables' values at run-time.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2013
Spoiler



Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: problem with insert records into table

Posted 30 July 2015 - 11:22 AM

If you're going to use DataSets, there's absolutely nothing wrong with using a DataAdapter. Readers are just a more "manual" way of doing it. It's more direct. OF course, just making an adapter does nothing. You need a DataSet to fill with the adapter.

@rgfirefly24, I think you're thinking of a SqlCommandBuilder. Adapters are for filling DataSets/DataTables.

Regardless, there's a lot wrong here. Please read the link in my signature about Databases. It will show you the basics on how to query them, as well as how to query them safely, which you very much are not doing at the moment.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1