How to store textbox value in database [Solved/Closed]

NewUser - Mar 30, 2010 at 02:55 AM - Latest reply:  sham
- Jan 26, 2015 at 08:34 PM


hello.. im a new user of asp.net.. im creating an asp.net webpage using VB code.

I wanted to know how to store the textbox value(input from user) into the sql databse table..
i need a sample codef or this.
im using ASP.NET 3.5, Visual Studio 2008, SQL server 2005.

Help is really appreciated.. thank you
See more 

22 replies

Best answer
49
Thank you
sqlconnection myconn=new sqlconnection();
myconn.connectionstring="-------specify path here------";
myconn.open();
string qry="insert into table1 values('"+username+"','"+password+"')";
sqlcommand cmd=new sqlcommand(qry,myconn);
cmd.ExecuteNonQuery();
myconn.Close();

Thank you, sushma 49

Something to say? Add comment

CCM has helped 1687 users this month

in this code missing where passing this value colm was missing
Hello mam.........
In where I can fetch the path
i am unable to save values in ms access database, when I upload the website in server
26
Thank you
sqlconnection myconn=new sqlconnection();
myconn.connectionstring="-------specify path here------";
myconn.open();
string qry="insert into table1 values('"+username+"','"+password+"')";
sqlcommand cmd=new sqlcommand(qry,myconn);
cmd.ExecuteNonQuery();
myconn.Close();
thanx nidhi ........
thanks .ur code is perfect.
please writ the code for vb to ms access database
it works thanks so much
thank u nidhi
15
Thank you
yes. I found the answer to this qn.
on my html page I have the textboxes where the user will input their data.
then they have to click on submite after filling the form.

i will attach sample code for you.

html code :

<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Place"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
<br />
<asp:Label ID="Label3" runat="server"></asp:Label>
</div>
</form>
</body>

asp.net code :

Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'assigning string with values of textbox.
Dim strName, strPlace As String
strName = TextBox1.Text
strPlace = TextBox2.Text
'declaring sql connection. please complete it by urself
Dim sqlcon As New SqlConnection("server=;database=;uid=;password=")

'checking if connection is open. if open close it and open again.
If sqlcon.State = ConnectionState.Open Then
sqlcon.Close()
End If
sqlcon.Open()
Dim strcommand As String
'the insert command is stored in strcommand
'Table Name : Contact
'Column of table : Name, Place
strcommand = "Insert into Contact(Name,Place) values ('" + strName + "','" + strPlace + "')"
Dim sqlcomm As New SqlCommand(strcommand, sqlcon)
Dim o As String = sqlcomm.ExecuteNonQuery()
label3.text = "Entry Added!"

End Sub
End Class
thanks that code is 2 much help me. thank u so much.
'checking if connection is open. if open close it and open again'..how? please teach me
13
Thank you
it will execute nicely.....
5
Thank you
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


public partial class _Default : System.Web.UI.Page
{



protected void Page_Load(object sender, EventArgs e)
{

}


protected void Button1_Click1(object sender, EventArgs e)
{


SqlCommand cmd;
SqlConnection con = new SqlConnection(@"Data Source=sts-pc\sqlexpress;Initial Catalog=master;Integrated Security=True;");
con.Open();
cmd = new SqlCommand("INSERT INTO login VALUES('" + TextBox1.Text + "', '" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')", con);
cmd.ExecuteNonQuery();

con.Close();
Response.Redirect("http://localhost:4162/WebSite8/Default3.aspx");
}
1
Thank you
thank you. my qn was answered. I was using vb and not c#.
0
Thank you
select record in database in c#.net codes