I have a textbox and a submit button . when i enter anything in the textbox i want it to be stored in the sqlserver
I have multiple columns in database of sqlserver.
how can i enter only one value from textbox and pass other column values as null.
I am new to .net so please help.
Antony
Loading
Pankaj PandeyPosted Jun 6, 2013, 4:31 AM
replace Mytable with your real table name
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con= new SqlConnection(@" Data Source = .\azmath1\SQLEXPRESS; Initial catalog =Category; Integrated Security= True");
SqlCommand cmd = new SqlCommand("insert into Mytable (PCategory) values('"+TextBox1.Text+"')",con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
and give responce...
antony victorPosted Jun 6, 2013, 12:38 PM
Pankaj PandeyPosted Jun 6, 2013, 5:31 AM
if error shows, then list here followings..
database name
server name (copy and paste server name when you start the sql server)
table name
table script....after that i will create code..you need to just copy and paste
antony victorPosted Jun 6, 2013, 5:27 AM
SqlConnection con = new SqlConnection(@" Data Source = .\AZMATH1; Initial catalog =Category; Integrated Security= True");
Sunny SharmaPosted Jun 6, 2013, 4:48 AM
you must change the connection string pointing to your own database.
Pankaj PandeyPosted Jun 6, 2013, 4:48 AM
then put your connection string and Mytable to table name
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con= new SqlConnection("Put your connection String Here");
SqlCommand cmd = new SqlCommand("insert into Mytable (PCategory) values('"+TextBox1.Text+"')",con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
antony victorPosted Jun 6, 2013, 4:41 AM
Line 26:
Line 27: SqlConnection con = new SqlConnection(@" Data Source = .\azmath1\SQLEXPRESS; Initial catalog =Category; Integrated Security= True");
Line 28: con.Open();
Line 29: String str = "insert into Mytable1 (PCategory) values('" + TextBox1.Text + " ')";
Line 30: SqlCommand cmd = new SqlCommand(str, con);
antony victorPosted Jun 6, 2013, 4:24 AM
Riddhi ValechaPosted Jun 6, 2013, 3:53 AM
Re-check the database design.
Check the "Allow Nulls" column if the database can accept null values...
If that column is not checked, then also the values will not be inserted...
Shankar MPosted Jun 6, 2013, 3:34 AM
Pankaj PandeyPosted Jun 6, 2013, 3:14 AM
anyway./..you do not need to replace "@x" with any thing..just copy and pase below code..
replace Mytable with your real table name
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con= new SqlConnection(@" Data Source = .\azmath1\SQLEXPRESS; Initial catalog =Category; Integrated Security= True");
SqlCommand cmd = new SqlCommand("insert into Mytable (PCategory) values('"+TextBox1.Text+"')",con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Sunny SharmaPosted Jun 6, 2013, 3:13 AM
antony victorPosted Jun 6, 2013, 3:08 AM
Pankaj PandeyPosted Jun 6, 2013, 2:47 AM
You do not need to pass null for others, it by default accept null
just try this..it will work, if not then write here.
SqlConnection conn=new SqlConnection("Your Connection String");
cmd.ExecuteNonQuery();
conn.Close();
Mark as answered if helpful
antony victorPosted Jun 6, 2013, 2:38 AM
my code is
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con= new SqlConnection(@" Data Source = .\azmath1\SQLEXPRESS; Initial catalog =Category; Integrated Security= True");
con.Open();
SqlCommand cmd = new SqlCommand("insert into Mytable (PCategory) values(@PCategory)",con);
cmd.CommandTimeout = 0;
cmd.Parameters.AddWithValue("@PCategory", TextBox1.Text);
cmd.ExecuteNonQuery();
con.Close();
}
but textbox value is not inserting into database
Pankaj PandeyPosted Jun 6, 2013, 2:05 AM
try this..
SqlConnection conn=new SqlConnection("Your Connection String");
cmd.ExecuteNonQuery();
conn.Close();
Mark as answered if helpful
Sunny SharmaPosted Jun 6, 2013, 1:53 AM
If all the rest columns are null by default then use this statement
INSERT INTO myTable(myColumn) Values(textBox.text);
Hope it helps :)