Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I created an apps which insert data to sql server.there is no problem in connecting to the server . but i want to use a dialog box when user enters same data which already been inserted to database .but i don't know why it is not working...
my code is here.....
Java
public void insert(){
       final String fname=name.getText().toString();
       final String fcity=city.getText().toString();
       ResultSet rs;
	 	 try{
	        	 Statement statement1=connect.createStatement();
	        	 rs=statement1.executeQuery("SELECT * FROM FORM1");
	        	 List<Map<string,string>>data1=null;
	        	 data1=new ArrayList<Map<string,string>>();
	        	 while(rs.next()){
	        	 Map<string,string>datanum1=new HashMap<string,string>();
	        		 datanum1.put("a", rs.getString("NAME"));
	        		 datanum1.put("b", rs.getString("CITY"));
	        		 data1.add(datanum1);
	        	 }
	        	 if(data1.contains(fname)!=false){
	        		 AlertDialog.Builder adb=new AlertDialog.Builder(context);
	       		 adb.setTitle("My Records");
	      	 adb.setMessage(fname+" Already exist... do u want to overwrite???");
	        		 adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	@Override
         public void onClick(DialogInterface dialog, int which) {
	// TODO Auto-generated method stub
	try{
	 Statement statement2=connect.createStatement();
	statement2.executeUpdate("UPDATE FORM1 SET NAME='"+fname+"',CITY='"+fcity+"' WHERE NAME='fname'");
	}catch(Exception e){
	Log.e("ERROR", e.getMessage());
	}
							
	}
	});
	adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
						
	@Override
	public void onClick(DialogInterface dialog, int which) {
	// TODO Auto-generated method stub
					
	}
	});
	AlertDialog adbc=adb.create();
	adbc.show();
	}
	else{
	Statement statement3=connect.createStatement();
	statement3.executeUpdate("INSERT INTO FORM1(NAME,CITY) VALUES('"+name.getText().toString()+"','"+city.getText().toString()+"')");
	}
     	}catch(Exception e){
        Log.e("ERROR", e.getMessage()); 
	}
}

if any one plzz tell me what code should i use
Posted
Updated 29-Jul-13 23:56pm
v4
Comments
Pratik Butani 12-Aug-13 2:33am    
create one method for displaying alert and pass title and message as an argument.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900