-3

I am a student and just start learning asp.net with C#.

I am using visual Studio 2015 and MSSQL Server 2012. I am facing a problem adding data in the database table from a webform.

I have established the connection successfully but couldn't be able to insert the data in the database table from a textbox.

I have search it at many places but couldn't able to do that.

So can anybody please tell me the simple and exact method to do that?

Tom
  • 1,387
  • 3
  • 19
  • 30
R.kumar
  • 1
  • 1
  • 4
  • Please, insert some code you've tried so far. Mostly your HTML form, your method to save into the database and the structure to your table. It helps people to help you. – Tom Oct 20 '16 at 10:41

3 Answers3

0

Kumar I am not sure if you facing issue in inserting any data into database or in capturing text from textbox and then inserting it into database. However i will try to address both the issue. for inserting into database you can visit Invalid column name sql error as it has already been answered

now to capture the text froim textbox you can make use of HtmlElementClass

HtmlElement txtBox = null;
    HtmlDocument doc = webBrowser1.Document;
    if (doc != null)
    {
        txtBox = doc.GetElementByID("TxtboxelementId");
        string txtValue=txtBox.InnerHtml
    }
Community
  • 1
  • 1
0

ASPX Page :

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
        <asp:Button ID="btnInsert" runat="server" Text="Button" OnClick="btnInsert_Click" />
    </div>
</form>

CS Code :

protected void Page_Load(object sender, EventArgs e)
    {

    }

    private void InsertValue(string value)
    {
        using (SqlConnection con = new SqlConnection("YOUR_CONNECTION_STRING")) //Creating connection object
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand("INSERT INTO YOUR_TABLE_NAME (Field_Name) VALUES (@value)", con))
                {
                    if (con.State == System.Data.ConnectionState.Closed) con.Open();
                    cmd.Parameters.AddWithValue("@value", value);   // Adding parameter with value to command object
                    int result = cmd.ExecuteNonQuery();             // Executing query and it returns no of rows affected.
                    if (result > 0) Response.Write("Successful.");  // Checking if no of rows affected is > 0 meaning value successfully inserted.
                }
            }
            catch (SqlException ex)  //Handling SQL Exceptions
            {
                this.LogErrors(ex);
            }
        }
    }

    private void LogErrors(Exception ex)
    {
        // Write error log logic here
    }

    protected void btnInsert_Click(object sender, EventArgs e) // insert data button click event handler
    {
        this.InsertValue(this.txtValue.Text); 
    }
-1
SqlConnection con= (Connection Name)

If you are using without primary key in Database please go ahead with this

SqlCommand query = new SqlCommand("Insert into TABLE values ("+txtsample1.Text+")",con)

If it's with primary key mention your field names

SqlCommand query = new SqlCommand("Insert into TABLE (column1) values ("+txtsample1.Text+")",con)
Pang
  • 9,564
  • 146
  • 81
  • 122
Jays
  • 90
  • 8