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 PandeyRank #642 · 3.7K reputationPosted Jun 6, 2013, 12:31 AM EDT
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...
Sunny SharmaRank #223 · 11.1K reputationPosted Jun 5, 2013, 9:53 PM EDT
If all the rest columns are null by default then use this statement
INSERT INTO myTable(myColumn) Values(textBox.text);
Hope it helps :)
Pankaj PandeyRank #642 · 3.7K reputationPosted Jun 5, 2013, 10:05 PM EDT
try this..
SqlConnection conn=new SqlConnection("Your Connection String");
cmd.ExecuteNonQuery();
conn.Close();
Mark as answered if helpful
antony victor90 reputationPosted Jun 5, 2013, 10:38 PM EDT
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 PandeyRank #642 · 3.7K reputationPosted Jun 5, 2013, 10:47 PM EDT
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 victor90 reputationPosted Jun 5, 2013, 11:08 PM EDT
Sunny SharmaRank #223 · 11.1K reputationPosted Jun 5, 2013, 11:13 PM EDT
Pankaj PandeyRank #642 · 3.7K reputationPosted Jun 5, 2013, 11:14 PM EDT
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();
}
Shankar MRank #874 · 2.5K reputationPosted Jun 5, 2013, 11:34 PM EDT
Riddhi ValechaRank #794 · 2.9K reputationPosted Jun 5, 2013, 11:53 PM EDT
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...
antony victor90 reputationPosted Jun 6, 2013, 12:24 AM EDT
antony victor90 reputationPosted Jun 6, 2013, 12:41 AM EDT
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);
Pankaj PandeyRank #642 · 3.7K reputationPosted Jun 6, 2013, 12:48 AM EDT
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();
}
Sunny SharmaRank #223 · 11.1K reputationPosted Jun 6, 2013, 12:48 AM EDT
you must change the connection string pointing to your own database.
antony victor90 reputationPosted Jun 6, 2013, 1:27 AM EDT
SqlConnection con = new SqlConnection(@" Data Source = .\AZMATH1; Initial catalog =Category; Integrated Security= True");
Pankaj PandeyRank #642 · 3.7K reputationPosted Jun 6, 2013, 1:31 AM EDT
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 victor90 reputationPosted Jun 6, 2013, 8:38 AM EDT