i want 2 pass values(enterd in textbox)to table in sql
server without using stored procedure in c#.plz tell me
code with an example.

Answers were Sorted based on User's Feedback



i want 2 pass values(enterd in textbox)to table in sql server without using stored procedure in c#..

Answer / bala

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs
e)
{
String ConnectionString = "Data
Source=ServerName;Initial Catalog=DatabaseName;Integrated
Security=true";
String Query = "insert into tablename values('"
+ textBox1.Text + "')";
SqlConnection Con = new SqlConnection
(ConnectionString) ;
Con.Open();
SqlCommand Cmd = new SqlCommand(Query, Con);
Cmd.ExecuteNonQuery();
Cmd.Dispose();
Con.Close();

}
}
}

Is This Answer Correct ?    10 Yes 2 No

i want 2 pass values(enterd in textbox)to table in sql server without using stored procedure in c#..

Answer / michael and tubax

sqlconnection con=new sqlconnection
("server=servername;datasource=databasename;uid=sa;pwd=passw
ord);
con.open();
sqlcommand cmd=new sqlcommand("insert into tblname values
('"+txtname.text+"','"+txtage.text+"'",con);
cmd.executnonquery();
con.close();

Is This Answer Correct ?    8 Yes 1 No

i want 2 pass values(enterd in textbox)to table in sql server without using stored procedure in c#..

Answer / ranjeet kumar panda

SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
SQlDataAdapter da;
string sqlstr;

Connection String......

//using DataReader

sqlstr="insert into tablename(Colname) values
("+textBox1.Text+")";
if(con.state==ConnectionState.Open)
con.Close();
cmd.CommandText=sqlstr;
con.Opem();
cmd.ExecuteNonQuery();

//using DataSet

.........
........
da.Fill(ds,"tablename");
DataRow drw=ds.Tables[0].NewRow();
drw[0]=textBox1.Text;
ds.Table[0].Rows.Add(drw);
SqlCommandBuilder cb=new SqlCommandBuilder(da);
da.Update(ds,"tablename");


//Using LinqToSql (.Net 3.5)

DataContextName dc=new DataContextName();
//Lets a Control to bind the data
DataGridView1.DataSource=dc.GetTable<tablename>;
//create an object of class(i.e table)
tablename obj=new tablename();
obj.colname=textBox1.Text;
dc.tablenames.InsertOnSubmit(obj);//'s' in tablenames
//is essential
dc.SubmitChanges();

Is This Answer Correct ?    7 Yes 3 No

i want 2 pass values(enterd in textbox)to table in sql server without using stored procedure in c#..

Answer / srinivas shahukaru

we havwe differnt ways to pass data to back end .we can pass
data in dis way also
cmd.Command Type=CommandType.Text;
then we can pass values values using inserrtquery in sql
server... insert into tablename values ('textbox1.text,...);

Is This Answer Correct ?    8 Yes 5 No

i want 2 pass values(enterd in textbox)to table in sql server without using stored procedure in c#..

Answer / sandeep singh shekhawat

Solution1:
SqlConnection con=new SqlConnection("Data Source=./SqlExpress;Database=Test;Integrated Security= true;")
SqlDataAdapter adp=new SqlDataAdapter("insert into employee values(@Id,@Name)",con);
dap.SelectCommand.Parameters.AddwithValue("@Id",TextBox1.Text);
dap.SelectCommand.Parameters.AddWithValue("@Name",TextBox2.Text);

for this sql Connection define globally and after that all code write on button click event.


Solution2
SqlConnection con=new SqlConnection("Data Source=./SqlExpress;Database=Test;Integrated Security= true;")
SqlCommand cmd=new SqlCommand("insert into emp values(@Id,@Name)",con)
cmd.Parameter.AddWithValue("@Id",TextBox1.Text);
cmd.Parameter.AddwithValue("@Name",TextBox2.Text);
if(con.State==ConnectionState.Closed)
con.Open();
cmd.ExecuteNonQuery();

Is This Answer Correct ?    6 Yes 3 No

Post New Answer

More ADO.NET Interview Questions

can we change the data in dataset? if yes what is the process?

1 Answers   Artech,


what is type dataset?

3 Answers  


I loaded the dataset with a table of 10 records. One of the records is deleted from the backend, how do you check whether all the 10 records were present while updating the data(which event and steps) and throw the exception 28 can datareader hold data from multiple tables?

0 Answers  


What is full form of ado?

0 Answers  


What is difference between Dataview and Datatable?

0 Answers  






What is the return type of executescalar?

0 Answers  


What are the parameters that control most of connection pooling behaviors?

0 Answers  


Which one of the following objects is a high-level abstraction of the connection and command objects in ado.net?

0 Answers  


What are the different row versions available?

3 Answers  


what is bubbled event can u pls explain

3 Answers   Wipro,


What are advantages and disadvantages of Microsoft-provided data provider classes in ADO.NET?

2 Answers  


Write steps of retrieving data using ado.net ?

3 Answers   Keane India Ltd,


Categories