1

I'm trying to add the values / the text the user input from my textboxs into my database.

At the moment I can't get the code to insert the values to the sqldatabase.

Here is my aspx Code

<asp:TextBox ID="txt_WineName" runat="server" PlaceHolder="WineName" />
<asp:TextBox ID="txt_WineYear" runat="server" PlaceHolder="WineYear" />
<asp:TextBox ID="txt_WinePrice" runat="server" PlaceHolder="WinePrice" />
<asp:TextBox ID="txt_WineType" runat="server" PlaceHolder="WineType" />
<asp:Button ID="btn_AddWine" runat="server" Text="Add" />

Here is my C# code:

protected void btn_AddWine(object sender, EventArgs e)
{
    using (SqlConnection connection = new SqlConnection("Data Source=PCM13812;Initial Catalog=Kronhjorten;Integrated Security=True"))
    {
        connection.Open();
        string query = "SELECT * FROM Wines";
        using (SqlCommand command = new SqlCommand(query, connection))
        { 
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read()) 
                {
                    string Name = txt_WineName.Text;
                    string Year = txt_WineYear.Text;
                    string Price = txt_WinePrice.Text;
                    string Type = txt_WineType.Text;
                    Sql_Insert.Insert();
                }
            }
        }
    }
}

I tried other links from stackoverflow but can't seem to find anything that makes this work.

Hope you can help me. I'm sorry if I'm doing this in a strange way.

Damon
  • 3,004
  • 7
  • 24
  • 28
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77

2 Answers2

6

The right commands:

First of all, you're using a SqlDataReader. This is not used for inserting data into a database but for reading data from it. You have to execute the SqlCommand you are using.

string query = "YOUR_QUERY_HERE";

using (SqlConnection connection = new SqlConnection("Data Source=PCM13812;Initial Catalog=Kronhjorten;Integrated Security=True"))
{
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        connection.Open(); 
        command.ExecuteNonQuery();
    }
}

Correct query:

When you get this right it's time to write a correct query. Yours starts with SELECT, again this is for retrieving data, not inserting. Your query should use INSERT and should look something like this:

string name = txt_WineName.Text;
string year = txt_WineYear.Text;
string price = txt_WinePrice.Text;
string type = txt_WineType.Text;

string query = "INSERT INTO Wines(Name, Year, Price, Type) " +
               "Values('" + name + "', '" + year + "', '" + price + "', '" + type + "')";

Parameters/validation:

The code I presented is only meant as a demo, not as working code. You should always validate user input and use parameterized queries. More info/reading:

Abbas
  • 14,186
  • 6
  • 41
  • 72
0

You are using SqlDataReader which is only to read data from database not to insert/update. Following links may help you

Insert Data into database in C#

and

How to insert Records in Database using C# language?

Community
  • 1
  • 1
SamTech
  • 1,305
  • 2
  • 12
  • 22