0

We have created a registration form in Matlab with some field such as name, age, etc. Also we have created a database using Sql Server(ODBC). Now we can read the TextBox values and display it in command window by get property (it is in pushbutton callback). We have to insert that textbox value in database which is already created We use fastinsert comMand. But its just add the values manualLy(using query) into it, but we want to add it through textbox. Our code in push button callback is here.

conn = database('Addface_DSN','sa','123 ');

if(isempty(conn.message))

     disp('database connected')

 else
     disp('cannot connected')

     disp(conn.message);

     return
end

setdbprefs('DataReturnFormat','numeric')

exdata = {'2','Shalu','22','female'};

fastinsert(conn, 'Faces_Details', {'Id' 'Name' 'Age' 'Gender' },exdata)

commit(conn)

close(conn);

name = get(handles.edit1, 'string'); % we dont want this, But just add to chek. we want this name in database.

disp(name)

age = get(handles.edit2, 'string'); 

disp(age)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Anjuzz
  • 3
  • 4
  • I didn't understood the ending part `We use fastinsert comMand. But its just add the values manualLy(using query) into it, but we want to add it through textbox` can you please explain? so that I can help you.. as I far as I know you are getting the data from text box `name and age`and you are able to send `ex2` data to database then what are you looking for? – Incpetor Jul 19 '14 at 07:20
  • Sorry...I mean we dont want to display it on command window by disp command. We have to pass that name and age in exdata(in fast insert) to insert into table. What to do..? pls..help us.@Inceptor361 – Anjuzz Jul 21 '14 at 04:54

1 Answers1

0

Please try this solution Most of the solution is your code only

conn = database('Addface_DSN','sa','123 ');

if(isempty(conn.message))

     disp('database connected')

 else
     disp('cannot connected')

     disp(conn.message);

     return
end

setdbprefs('DataReturnFormat','numeric')

// Here is the difference

// Taking test_age as 'int' 
test_name=get(handles.edit1, 'string'); 
test_age= get(handles.edit2, 'string');
//Convert to int and then insert
test_age=str2num(test_age);
exdata2={'3',test_name,test_age,'Male'};

exdata = {'2','Shalu','22','female'};

//Here you insert the data.
fastinsert(conn, 'Faces_Details', {'Id' 'Name' 'Age' 'Gender' },exdata2)

commit(conn)

close(conn);

name = get(handles.edit1, 'string'); % we dont want this, But just add to chek. we want   this name in database.

disp(name)

age = get(handles.edit2, 'string'); 

disp(age)
Incpetor
  • 1,293
  • 6
  • 25
  • 46
  • Thank You..for your response. But we already tried it. we got an error. That is: No method 'setInt' with matching signature found for class'sun.jdbc.odbc.JdbcOdbcPreparedStatement'. What to do. Pls help. I think there may be need some conversions of datatypes.In the table Name is nvarchar and age is int type. – Anjuzz Jul 21 '14 at 08:22
  • as the data type in database is `int` try this. `test_age=str2num(test_age)` Add this line. before the exdata2. – Incpetor Jul 21 '14 at 10:31
  • It works. Thank you...Do you know how to pass image field into it.? or how to convert image into binary. @Inceptor361 – Anjuzz Jul 22 '14 at 04:11
  • I have updated the code above. Please accept the answer if it works so , it would be helpful to others as well. and regarding your other doubt. post another question. – Incpetor Jul 22 '14 at 07:37