Friday, March 9, 2012

defining command,commandtype and connectionstring for SELECT command is not similar to INS

i am using visual web developer 2005 and SQL 2005 with VB as the code behind

i am usingINSERT command like this

Dim test As New SqlDataSource()
test.ConnectionString = ConfigurationManager.ConnectionStrings("DatabaseConnectionString1").ToString()
test.InsertCommandType = SqlDataSourceCommandType.Text
test.InsertCommand = "INSERT INTO try (roll,name, age, email) VALUES (@.roll,@.name, @.age, @.email) "

test.InsertParameters.Add("roll", TextBox1.Text)

test.InsertParameters.Add("name", TextBox2.Text)
test.InsertParameters.Add("age", TextBox3.Text)
test.InsertParameters.Add("email", TextBox4.Text)
test.Insert()

i am usingUPDATE command like this

Dim test As New SqlDataSource()
test.ConnectionString = ConfigurationManager.ConnectionStrings("DatabaseConnectionString").ToString()
test.UpdateCommandType = SqlDataSourceCommandType.Text
test.UpdateCommand = "UPDATE try SET name = '" + myname + "' , age = '" + myage + "' , email = '" + myemail + "' WHERE roll 123 "
test.Update()

but i have to use theSELECT command like this which is completely different from INSERT and UPDATE commands

Dim tblData As New Data.DataTable()
Dim conn As New Data.SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
Dim Command As New Data.SqlClient.SqlCommand("SELECT * FROM try WHERE age = '100' ", conn)
Dim da As New Data.SqlClient.SqlDataAdapter(Command)
da.Fill(tblData)
conn.Close()

TextBox4.Text = tblData.Rows(1).Item("name").ToString()
TextBox5.Text = tblData.Rows(1).Item("age").ToString()
TextBox6.Text = tblData.Rows(1).Item("email").ToString()


for INSERT and UPDATE commands defining the command,commandtype and connectionstring is same

but for the SELECT command it is completely different. why ?

can i define the command,commandtype and connectionstring for SELECT command similar to INSERT and UPDATE ?

if its possible how to do ?

please help me

Hi you can use:

Insert:

Dim

conAsNew SqlConnection("server=.;database=test;uid=sa")Dim cmdAsNew SqlCommand

con.Open()

cmd.Connection =con

cmd.CommandType =Data.CommandType.Text

cmd.CommandText ="insert into t1(name,birth) values (@.n,@.b)"

cmd.Parameters.Add(

New SqlParameter("@.n", Data.SqlDbType.NVarChar)).Value =TextBox1.Text

cmd.Parameters.Add(

New SqlParameter("@.b", Data.SqlDbType.DateTime)).Value =TextBox2.Text

cmd.ExecuteNonQuery()

con.Close()

Update:

Dim conAsNew SqlConnection("server=.;database=test;uid=sa")

Dim cmdAsNew SqlCommand

con.Open()

cmd.Connection =con

cmd.CommandType =Data.CommandType.Text

cmd.CommandText ="updae t1 setname= @.n , birth= @.b)"

cmd.Parameters.Add(

New SqlParameter("@.n", Data.SqlDbType.NVarChar)).Value =TextBox1.Text

cmd.Parameters.Add(

New SqlParameter("@.b", Data.SqlDbType.DateTime)).Value =TextBox2.Text

cmd.ExecuteNonQuery()

con.Close()

Select:

Dim conAsNew SqlConnection("server=.;database=test;uid=sa")

Dim cmdAsNew SqlCommand

con.Open()

cmd.Connection =con

cmd.CommandType =Data.CommandType.Text

cmd.CommandText ="updae t1 setname= @.n , birth= @.b)"

Dim da As New Data.SqlClient.SqlDataAdapter(Command)
da.Fill(tblData)

con.Close()

|||Sorry the third query is "select .........." not "update ...."

No comments:

Post a Comment