473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Inserting Records

3 New Member
Hai all

I wrote a common piece of code for inserting the records in a table using c#, i stored the control names in a Database table with the type of control, and the table field name will be same as control name so i retrived the values and build the string like
str1= "insert tablename colmn1,colmn2 values (cntrlname1.txt,cntrlname2.txt)"

cn.Open();
cmd.Connection = cn;
cmd.CommandText = str1.ToString();
cmd.ExecuteNonQuery();
cn.Close();

The above code inserts directly(cntrlname1.txt,cntrlname2.txt) instead of cntrlname1.txt,cntrlname2.txt values, but if i copy the str1 value and put

cmd.CommandText="insert tablename colmn1,colmn2 values (cntrlname1.txt,cntrlname2.txt)"

it correctly inserts the value of cntrlname1.txt,cntrlname2.txt

Please provide me some solution
Mar 17 '07 #1
5 1255
kenobewan
4,871 Recognized Expert Specialist
Welcome the site. By converting str1 to string you are converting the .text to string rather than allowing it to represent the value as a property.
Mar 17 '07 #2
kannaworld
3 New Member
Welcome the site. By converting str1 to string you are converting the .text to string rather than allowing it to represent the value as a property.

Expand|Select|Wrap|Line Numbers
  1. cn.Open();
  2.             cmd=new SqlCommand("Select Ctrl_Name,Ctrl_Type from VCC_FieldMaster",cn);
  3.             dr=cmd.ExecuteReader();
  4.             while(dr.Read())
  5.             {
  6.                 fld += dr[0].ToString() + ",";
  7.                 fld1 +="'\"+ ";                                    
  8.                 fld1 += dr[0].ToString();
  9.                 val += dr[1].ToString()+ ",";
  10.  
  11.                 if (dr[1].ToString()=="TT") //TT represent Textbox
  12.                    {                    
  13.                     fld1 += ".Text" + " +\"'" + ",";        
  14.  
  15.                 }    
  16.                 else if (dr[1].ToString()=="DD") //DD represents Dropdown box
  17.                 {
  18.                     fld1 += ".SelectedIndex" + " +\"'" + ",";
  19.                 }
  20.             }
  21.             cn.Close();
  22.             fld1 = fld1.Remove(fld1.Length-1,1);
  23.             fld = fld.Remove(fld.Length-1,1);
  24.             qq ="Insert into VCC_Sample(Nam,EmpNo) values";
  25.             qq+="(";
  26.             qq +=  fld1 ;
  27.             qq+=")";
  28.             Response.Write(qq);
  29.             cn.Open();
  30.             cmd.Connection = cn;
  31.             cmd.CommandText = qq;
  32.             cmd.ExecuteNonQuery();
  33.             cn.Close();
this is my coding although i avoided .Tostring() it does the same!!!
Mar 20 '07 #3
kenobewan
4,871 Recognized Expert Specialist
You have the same problem - try:
Expand|Select|Wrap|Line Numbers
  1. qq ="Insert into VCC_Sample(Nam,EmpNo) values";
  2. qq+="(";
  3. qq += "'" & fld1 & "'";
  4. qq+=")";
Mar 20 '07 #4
kannaworld
3 New Member
You have the same problem - try:
Expand|Select|Wrap|Line Numbers
  1. qq ="Insert into VCC_Sample(Nam,EmpNo) values";
  2. qq+="(";
  3. qq += "'" & fld1 & "'";
  4. qq+=")";


This is the buid error i get on using that

Operator '&' cannot be applied to operands of type 'string' and 'string'
Mar 20 '07 #5
kenobewan
4,871 Recognized Expert Specialist
Try + and some debugging.
Mar 20 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Pushpendra Vats | last post by:
Hi , I am trying to insert records into database. I am trying to write the following code. On button1 click event i am inserting five records and after that i am calling the update method of...
0
by: Pushpendra Vats | last post by:
Hi , I am trying to insert records into database. I am trying to write the following code. On button1 click event i am inserting five records and after that i am calling the update method of...
6
by: Pushpendra Vats | last post by:
Hi , I am trying to insert records into database. I am trying to write the following code. On button1 click event i am inserting five records and after that i am calling the update method of...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
5
by: rando1000 | last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record. ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.