passing runtime queries to DB...

Hai,

I'm using these codings to pass  values to insert query during runtime i.e; from TextBox control. Given here is partial coding, anyone pls give me a sample coding just to insert atleast 2 values to db Table from the user.

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Data.OleDb;

namespace Student {

class InsertDemo: System.Windows.Forms.Form {//start of class

private System.Data.OleDb.OleDbConnection myCon;

private void InitializeComponent(){

this.myCon = new System.Data.OleDb.OleDbConnection();

this.myCon.ConnectionString = @"Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=Saami;Data Source=sqltech-20;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=FAC-1;Use Encryption for Data=False;Tag with column collation when possible=False";

...

}

private void button1_Click(object sender, System.EventArgs e)

{

OleDbCommand ins=new OleDbCommand("insert into Student(regno, studName, mark1, mark2, mark3, mark4) "+

"values (@regno, @studName, @mark1, @mark2, @mark3, @mark4", myCon);

ins.Parameters.Add("@regno", OleDbType.VarChar,10);

ins.Parameters["@regno"].Value=textBox1.Text;

ins.Parameters.Add("@studName", OleDbType.VarChar,15);

ins.Parameters["@studName"].Value=textBox2.Text;

ins.Parameters.Add("@mark1", OleDbType.Integer);

ins.Parameters["@mark1"].Value=Int32.Parse(textBox3.Text);

ins.Parameters.Add("@mark2", OleDbType.Integer);

ins.Parameters["@mark2"].Value=Int32.Parse(textBox4.Text);

ins.Parameters.Add("@mark3", OleDbType.Integer);

ins.Parameters["@mark3"].Value=Int32.Parse(textBox5.Text);

ins.Parameters.Add("@mark4", OleDbType.Integer);

ins.Parameters["@mark4"].Value=Int32.Parse(textBox6.Text);

int res=0;

try

{

ins.Connection.Open();

res=ins.ExecuteNonQuery();

ins.Connection.Close();

textBox1.Text=res.ToString()+" records inserted";

}

catch{}

}

}//class

}//namespace

 

THANKS....




Answer this question

passing runtime queries to DB...

  • Andy Ho

    I don't understand what is the problem. Did your command executes or not I don't know also why are you using oledb when you connect to sql server. And i believe that when using oledb, mark for parameters is not @ but it is ' '.

  • passing runtime queries to DB...